Java Reference
In-Depth Information
The difference between sleeping and yielding is that a yielding thread never knows how
brief or long the wait will be. A sleeping thread, on the other hand, always knows that it will
wait for at least the amount of time specified.
Child.java Example
This example is probably more complicated than anything you'll have to do on the SCJD
exam, but it provides a good illustration of waiting. Study it carefully and you won't be over-
whelmed by the threading demands of the SCJD exam.
The first 41 lines in this example are very straightforward. An IceCreamMan object is
created in its own thread and goes into a loop, waiting for clients to hand him IceCreamDish
objects. The IceCreamMan is a static object, which ensures that there will only ever be one
IceCreamMan for all the children in our example.
In line 15 we set the IceCreamMan thread to be a daemon thread. The JVM will exit if the
only threads still running are daemon threads. In this example, we have explicitly created sev-
eral non-daemon threads: the three threads for each of the children. There is also one other
non-daemon thread that was created for us: the main thread. When these four threads have
completed, the only thread we explicitly created that is still running will be the IceCreamMan
daemon thread, so the program will terminate.
Note You cannot change a thread's daemon status after the thread has started—if you want your thread
to be a daemon, you must explicitly set it as such before starting the thread. A thread created from non-
daemon threads defaults to being a non-daemon thread. Likewise, a thread created from daemon threads
defaults to being a daemon thread. In either case, if you do not like the default type of thread, you can
change it by calling the setDaemon method on the thread.
The IceCreamMan class extends the Thread class, so we can call the start method directly, as
shown in line 16.
Line 25 shows an example of using the enhanced for loop to iterate over the items in an
array. In this array we create several Child objects, each of which is an independent thread.
Their role is to request a dish of ice cream from the IceCreamMan .
Lines 27 and 28 show how to create a thread based on a Runnable class. In line 27 we cre-
ate a new thread from the Runnable class, and in line 28 we start the thread as we would any
other thread object.
For this example we have decided that getting ice cream for our three children is the most
important task—what happens after that is not important. So we have decided that we can
end the application once all three children have eaten their ice cream. To do this, our main
thread must pause until all three Child threads have completed running. We accomplish this
in lines 32 through 38, where we call the join method on each of the child threads.
Tip If you are having trouble understanding the terminology of “joining a thread,” it may make more
sense if you refer back to Figure 4-2. In the diagram it appears as if each thread split from the main thread,
then rejoined it after completion.
Search WWH ::




Custom Search