Java Reference
In-Depth Information
66 /**
67 * build in a delay
68 */
69 private void delay() {
70 try {
71 System.out.println("IceCreamMan: delayed");
72 Thread.sleep((long) (Math.random()*1000) );
73 } catch (InterruptedException ie) {
74 ie.printStackTrace();
75 }
76 }
77 }
As Figure 4-3 shows, the IceCreamMan is waiting for clients in his own thread. Each Child
object is also in its own thread. The Child objects interact with the IceCreamMan by giving him
their IceCreamDish , and then they step out of the CPU and wait . The IceCreamMan prepares the
IceCreamDish , which is the signal that the Child associated with that IceCreamDish needs to
wake up and eat their IceCream .
Figure 4-3. IceCreamMan waiting for clients
Waiting Summary
A thread that needs a resource becomes paused without you specifically requesting that it
pause. In contrast, a thread that pauses because of a call to yield , wait , or sleep is paused
specifically at the request of the programmer. Regardless, all cases result in the thread pausing
execution.
In the case of a thread paused until a resource becomes available and a thread that
yielded to other threads, the programmer does not know when the thread will resume pro-
cessing—for the former, it will be whenever the resource becomes available, and for the latter
it will be at the discretion of the JVM thread scheduler. If you call wait without any parame-
ters, you will not know when the thread will resume processing—it is dependent on another
thread notifying it that it can continue operating. However, if you call wait with a time limit,
Search WWH ::




Custom Search