3.6 Why Passive Objects Are Components

 < Day Day Up > 



3.6 Why Passive Objects Are Components

The BoundedBuffer object in the previous program is obviously a component because it provides a service for and coordinates between the producer and consumer thread. In Chapter 1, five characteristics of a component were given. The following looks at how the bounded buffer meets all of these criteria.

  1. The bounded buffer object is not created in the producer or the consumer thread but is created in the controlling objects and is registered with those objects through a parameter passed in the constructor for the thread object.

  2. The bounded buffer communicates with the other objects through a standard method call procedure.

  3. The bounded buffer coordinates between the producer and consumer threads by implementing a state inside the bounded buffer and allowing transitions according to the rules in the state diagram used to design the bounded buffer.

  4. Race conditions are controlled for in the bounded buffer by using the monitor techniques described for the BinarySemaphore class in Chapter 2. Only one object can execute inside any Bounded-Buffer method at any time. Because all the wait calls are part of the precondition processing and do not occur once the program begins to modify the object data (the object state and circular queue variables), the state of the buffer is guaranteed and will be safe. This shows the advantage of separating the pre-condition processing from the rest of the program. Often, programmers who build passive objects by reasoning about their behavior are tempted to put processing inside of the pre-condition, but a wait exists in the pre-condition. If internal variables and the state can be changed in the pre-condition, then dropping the object lock causes a race condition and the program is unsafe. Using the precondition only to check to see when the method can be run ensures that a wait condition will not drop a lock, causing a possible race condition.

  5. The absence of deadlock if the bounded buffer component is used correctly can be easily demonstrated by examining the state diagram and its properties. This state diagram has the property that it is strongly connected; that is, every node can be reached by some path from every other node. Therefore, no states can be entered into where an active object will be stuck. The two possibilities of deadlock are (1) having the component permanently enter a FULL or EMPTY state, and (2) using this component with another component in such a manner as to produce a circular deadlock situation. Both of these conditions could only be caused by an error in the program using the component (i.e., have more takeFromBuffer calls than putToBuffer calls), so the component is safe if used properly.



 < 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