Java Reference
In-Depth Information
different delay value passed as the third argument. All objects of the class TryThread are daemon threads
because you call setDaemon() with the argument true in the constructor. Because the output can continue
indefinitely, you display a message to explain how to stop it.
After you've created a thread, it doesn't start executing by itself. You need to set it going. As I said earlier,
you don't call the run() method for the Thread object to do this, you call its start() method. Thus, you
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 and 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 thread execution. The code in this case is a single, infinite while
loop that you have put in a try block because the sleep() method that is called in the loop can throw
the InterruptedException exception that is caught by the catch block. The code in the loop outputs the
first name, calls the sleep() method that is 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.
With a multicore processor as many PCs have these days, multiple threads will be executing concurrently.
Each time a thread calls the sleep() method, one of the waiting threads jumps in and starts executing. 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 and the number
of processors that you have, 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 ex-
ecute the return . Executing return ends the thread for main() , and because 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() .
Stopping a Thread
If you did not create the threads in the last example as daemon threads, they would continue executing in-
dependently of main() . If you are prepared to terminate the program yourself (use Ctrl+C in a Windows
command-line session running Java), you can demonstrate this by commenting out the call to setDaemon()
in the constructor. Pressing Enter ends main() , but the other threads continue indefinitely.
A thread can signal another thread that it should stop executing by calling the interrupt() method for
that Thread object. This in itself doesn't stop the thread; it just sets a flag in the thread that indicates an inter-
ruption has been requested. This flag must be checked in the run() method to have any effect and the thread
should then terminate itself. The isInterrupted() method that the Thread class defines returns true if
the interrupted flag has been set. The method does not reset the flag, but calling the interrupted() method
tests the flag and resets it if it was set.
As it happens, the sleep() method checks whether the thread has been interrupted and throws an In-
terruptedException if it has been. You can see that in action by altering the previous example a little.
Search WWH ::




Custom Search