Java Reference
In-Depth Information
A network server might be managed overall by a user thread that starts one or more daemon threads to listen
for requests. When the server starts up, the operator starts the management thread and this thread creates
daemon threads to listen for requests. Each request recognized by one of these daemon threads might be
handled by another thread that is created by the listening thread, so that each request will be handled
independently. Where processing a transaction takes a finite time, and where it is important that the requests
are completed before the system shuts down, the thread handling the request might be created as a user
thread to ensure that it runs to completion, even if the listening thread that created it stops.
When the time comes to shut the system down, the operator doesn't have to worry about how many
listening threads are running. When the main thread is shut down, all the listening threads will also shut
down because they are daemon threads. Any outstanding threads dealing with specific transactions will
then run to completion.
Note that you can only call setDaemon() for a thread before it starts; if you try to do so afterwards,
the method will throw an IllegalThreadStateException exception. Also, a thread that is itself
created by a daemon thread will be a daemon by default.
Creating Thread Objects
In the method main() , we create three Thread variables that store three different objects of our class
TryThread . As you can see, each object has an individual name pair as the first two arguments to its
constructor, and a different delay value passed as the third argument. All objects of the class
TryThread are daemon threads because we call setDaemon() with the argument true in the
constructor. Since the output can continue indefinitely, we display a message to explain how to stop it.
Once you've created a thread, it doesn't start executing by itself. You need to set it going. As we said
earlier, you don't call the run() method for the Thread object to do this, you call its start()
method. Thus we start the execution of each of the threads represented by the objects first , second ,
and third by calling the start() method that is inherited from Thread for each object. The
start() method starts the object's run() method executing, then returns to the calling thread.
Eventually all three threads are executing in parallel with the original application thread, main() .
Implementing the run() Method
The run() method contains the code for the thread execution. The code in this case is a single, infinite
while loop which we put in a try block because the sleep() method that is called in the loop can
throw the InterruptedException exception caught by the catch block. The code in the loop
outputs the first name, calls the method sleep() inherited from Thread , and then outputs the second
name. The sleep() method suspends execution of the thread for the number of milliseconds that you
specify in the argument. This gives any other threads that have previously been started a chance to
execute. This allows the output from the three threads to become a little jumbled.
Each time a thread calls the method sleep() , one of the other waiting threads jumps in. You can see
the sequence in which the threads execute from the output. From the names in the output you can
deduce that they execute in the sequence first , second , third , first , first , second , second ,
first , first , third , and so on. The actual sequence depends on your operating system scheduler so
this is likely to vary from machine to machine. The execution of the read() method that is called in
main() is blocked until you press Enter , but all the while the other threads continue executing. The
output stops when you press Enter because this allows the main thread to continue and execute the
return . Executing return ends the thread for main() , and since the other threads are daemon
threads they also die when the thread that created them dies, although as you may have seen, they can
run on a little after the last output from main() .
Search WWH ::




Custom Search