9.9 Problems

 < Day Day Up > 



9.9 Problems

  1. Extend the first-in/first-out binary semaphore (FIFOBS) to be a counting semaphore (a so-called first-in/first-out counting semaphore, or FIFOCS).

  2. The following code fragment is in Exhibit 3 (Program9.2):

     if (! (locked || waiting.size() > 1)) waitObject = null; 

    If the object was locked or objects were already waiting, the current thread's wait object would not be null, and the thread would be forced to wait. In Exhibit 3 (Program9.2) are both needed to implement this test? If not, which part of the check can be removed? Can the "if" test always be reduced, as in this case? For example, consider the general case when multiple states could be present in the program.

  3. Show that no harmful race condition will occur as a result of dropping and reacquiring the synchronized lock on the "this" object in the getLock method of Exhibit 3 (Program9.2). Does the same hold if the FIFOCS in Problem 9.1 is implemented following the same basic design?

  4. Exhibit 3 (Program9.2) suggests a way to generalize the implementation of notification objects. Formalize this methodology. What are some of the limits placed on the state diagrams when this is done?

  5. Change the readers/writers object to implement the notification object using the methodology you developed in Problem 9.4.

  6. Rewrite the readers/writers buffer so that it does not have an inner class for confinement but is a single object. This can be done by making the beginRead, endRead, doRead, requestWrite, and doWrite methods private, so that the only access to the object is through the read and write methods. Compare this solution with the one using a separate private object and confinement.

  7. Change the readers/writers object in Program9.3 (Exhibits 5 and 6) to handle multiple writers after all readers have been processed.

  8. Change the gas station simulation in Program9.4 (Exhibits 10 through 15) to handle three pumps. Change it to handle four pumps. Change it to handle 100 cars. What is required for these changes?

  9. What are magic numbers, and why should they not be used in a program? How can you get around using a magic number?

  10. In the gas station simulation in Section 9.5 it was pointed out that, if multiple pumps became free at once, the pumps might not be given to the cars in the order in which they were freed. A solution was also proposed involving storing the pump with the notification object. Implement this solution.

  11. Why did the methods of the Pump class not have to be synchronized in Program9.4 (Exhibits 10 through 15)? How are the threads guaranteed to be safe when used by multiple car threads if the methods are not synchronized?

  12. Explain why it is a bad idea to allow a thread to run through several components. Put a separate thread in the pump object to do the notifications when the pump is free. Explain why this is a better solution to this problem.

  13. Complete the animator to handle repaint events correctly. This requires changing the application object.

  14. Change the ControlPanel so that if the speed controller is reset it does not automatically call move if the timeout has not occurred.

  15. Change the gas station program so that the pump manager can turn the pumps off at the end of the day. Make sure that no cars remain at a pump before turning it off.

  16. Change the gas station program so that multiple pump listeners (instances of pump manager) are listening on a pump. When a pump becomes free, the pump notifies all pump managers. When the pump is taken, the pump should notify all pump managers. However, to prevent a race condition where more than one pump manager tries to allocate the pump, the pump manager should first check the pump to see if it is taken. If not, the pump manager locks the pump before giving it to the next car. This is similar to the hardware TSET instruction. Before the car leaves, it should notify that the pump is now free.

  17. Implement a solution to the readers/writers problem using the Java Event Model, similar to the one used in the gas station simulation.

  18. Implement the gas station simulation assuming that the use of the pumps can have a priority bias.

  19. Section 9.5 discussed the race condition involving pumps being given to the cars and suggested that a solution would be to put the correct pump in the notification object. Implement this solution to the problem.



 < Day Day Up > 



Creating Components. Object Oriented, Concurrent, and Distributed Computing in Java
The .NET Developers Guide to Directory Services Programming
ISBN: 849314992
EAN: 2147483647
Year: 2003
Pages: 162

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net