Chapter 9: Combining Concurrent Techniques

 < Day Day Up > 



9.1 Introduction

Each of the techniques that have been covered so far, such as the use of state diagrams, Java Events, and notification objects, as well as such concepts as race conditions and deadlock, is very powerful in its own right; however, it is when these are used together and combined with other object-oriented programming (OOP) concepts, such as confinement, that truly elegant yet simple program designs and implementations can be achieved. Problems that would be difficult, if not impossible, to effectively realize using other paradigms are easily decomposed into cooperating, asynchronous activities. This chapter solves four problems to illustrate some of the ways in which combining concurrent programming techniques can produce elegant and effective components. On first trying to understand these solutions, they may seem complex; however, when the interactions between the components and asynchronous activities are isolated and the components are seen as providing a single coherent service to those asynchronous activities, the complexity within the programs can be viewed as being much less than would be required with some other implementation mechanisms. These simple components can then be used as building blocks for larger, more complex systems.

The four programs covered in this chapter are:

  1. First-in/first-out binary semaphore, which is a fair implementation of a binary semaphore. As was explained in Chapter 2 the wait/ notify in Java is not fair (i.e., it does not necessarily give the semaphore to the thread that has been waiting the longest). This implementation of the binary semaphore is fair in that the thread that has been waiting the longest is given the semaphore. It changes the way a state diagram is used to model a component by allowing the methods to work with Notification Objects to notify a specific thread.

  2. Readers/writers problem, where a number of threads are simultaneously trying to read and write to a buffer. Because readers do not modify the buffer, an unlimited number of readers can read the buffer simultaneously but only one writer can use the buffer, and then only if there are also no readers. The solution to this problem takes advantage of a state diagram design of the system, as well as a notification object, object confinement, and a totally synchronized object, to ensure a safe implementation of this component.

  3. An expansion of the gas station simulation from Chapter 3 has multiple pumps and one queue for cars waiting for a pump. This problem uses the Java Event Model and a state diagram for the design of a pump, as well as the ability to pass an object to other objects for processing.

  4. A proper controller for the animator. The controller in the animator suffers from a number of problems. The worst problem is that the control of the animator from the control panel is really a hack that tightly couples the animator to the controller of the animator. This is fixed by using interfaces to separate the controller from the animator and by using the techniques from Chapter 3 to implement an object that defines and encapsulates the interactions required to control the animator.



 < 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