You are on page 1of 3

c  

 

  .

   .

 .

        .

   .

              .

.

inside. p. be sure to include the name of the thread and a timestamp relative to the start time of the program. gates to the castle. and free spaces for each entrance should all be read as command line arguments using the following format: java Project1 [-n <#N>] [-m <#M >] Note that all command line arguments are optional. Also. State clearly what the current thread executing the print statement is doing. where N=2. and M=3. defenders. If they are not provided at run time. N*M+4 defenders. It is important to have print statements before. Use the p method and appropriate  statements to show how synchronization works. and after critical sections. your program should default to: N*M+4 attackers.        The number of attackers.

If the castle defense value ever goes to 0. the defenders barricade it once more and king has to wait for another opportunity. [ [  p p pp  p p : attackers overwhelm defenders. On this particular day. As soon as that attacker gets to that gate. he will try to escape. then the King has succeeded in escaping and the attackers lose. The number of spaces is equal to the number of people that can hold the gate closed on one side and equal to the number of people trying to break through the gate on the other side. The King will use the ÷ait(long)method instead of the sleepmethod to simulate the escape time. The combined attack/defend values will be compared with the following conditions below. he will try to attack that gate first. then the King will be signaled on the monitor ÷ait(long) he was called on and will retreat back into the castle. when a gate is cleared of attackers. in  (first come first serve) order. The defenders of the castle can be assigned this value at runtime because they are already armed. Use the Gate as an object that both defenders and attackers will wait on. Upon receiving a weapon an attacker will head towards a gate and prepare to attack (see Additionalotesfor selecting a gate). There's a . If an attacker arrives during the time the King takes to escape. the attacker will wait for all of the defenders to arrive at the appropriate gate. For each entrance there are free spaces. the defenders will each choose a gate and wait for all of the attackers to arrive at their gate. Upon arrival at the gate. The attackers will.  p A castle starts with a certain number of defense points (number of defense points is explained later in the [ [section). Likewise.   p p  p p pp : attackers are overwhelmed by defenders and the gate is considered clear of attackers. they have breached the outer castle walls and have taken over the armory. There are þgates to a castle. If the gate at which the King is escaping through doesn¶t have a new attacker when the escape time elapses. Simulate the time it takes to escape by having the King sleep some time. Villagers have gotten angry at the sudden increase in taxes the King has set. Once all of the defenders and attackers are at the appropriate gate the battle can commence. take a weapon from the armory and be assigned an attack random value between and at the time the weapon is obtained. The King is a very sneaky person. the castle falls. Whenever an attacker that is coming to the siege sees a gate that was cleared of his fellow attackers.

) If the castle¶s defense value is at or below 0. their defense variable¶s sum may equal 500.number which represents the total castle defense. the defenders lose. If several attackers (with a total attack value of 50) breach one of the gates. Additionalotes If no space to defend/attack is available at any gate then defenders/attackers just wait for free space (in  p  . then the castle defense value is down to 450. (Essentially. Those attackers that were able to breach the gate and the defenders that failed to protect the gate start attacking/defending again from the beginning. which means the castle¶s defense is also 500. the sum of their attack value is subtracted from the total castle defense (For example. It is equal to the sum of every single defender¶s defense value. if we have 100 defenders. When attackers breach a gate. their threads run in an infinite ÷   loop).

  .   .

The King¶s thread calls Castle¶s methods: ÷p       .  ). In the beginning every attacker/defender searches for the least attacked/defended gate to help out his friends. the battle is restarted from the beginning. If the attacking and defending values are equal (which is rare).

p .

p .

p Attackers/Defenders call Castle¶s methods: //if no space available just wait  p  .

     .

   //wait until all spaces are filled with attackers/defenders on both sides of the gate p p .

// rest random time  .

// rest random time .

  p.

.

!p .

If all attack/defend free spaces at the gates are occupied. There will be 2 lines. attackers/defenders wait in the order of their attack/defend value. ³ p#$. Use object named ³ ´ which is used by escaping king (to see if any gate is free) and attacking soldiers (toe ifanygatesarenotatta edatall). Use object named ³" ´ for king¶s thread to wait on. stronger attackers/defenders will get in front of the line (pi anydatastrutureyouli e). Each attacker/defender gets an object to wait on in that data structure.

´ and ³#$.

´. no thread should be allowed to get to any of the gates that is fully occupied. The last thread to arrive at a gate notifies all waiting threads on the gate. They all go through . For each gate there is an object named ³%p †(³Gate0† †Gate1† ) with 2M free spaces (foratta ers fordefenders).

  p.

.

!p .

The other threads wait on object ³&p  $.method where the last thread does the comparison.

 .

There may be other companies of threads that just finished attacking/defending and want to do the comparison too       !"     #     .´ until the comparison is done. We will name a group of attackers/defenders gathered on one site of a gate ± company.

 .

.

    .

                .

   .

  ùou should use some identification for the same company (forexample timestampall ompanytreadsandassignompanygatenumber timestamp+gate#÷ouldserveas .

gate is as it was before siege began).eatread÷ouldarrytatid÷itinitself).Y . When a company is done with the comparison their gate status is either breached or open for the king to try to escape (if attack/defend values matched. ùou can create any other methods/objects you may need. Once the comparison is done and the threads start notifying their respective reserve threads(first on line) and then rest.uniqueidforompany. After rest they join reserves themselves.