Changing the Running State of a Thread

   

Threads have a number of possible states as you saw previously in "The Life Cycle of a Thread." Take a look at how to change the state of a thread and cause it to enter these states and what the effects are. The methods covered here are

  • sleep(long), sleep(long,int)

  • yield()

  • destroy()

You have already briefly looked at the sleep method in the SimpleThread example from the previous section. Putting a thread to sleep essentially tells the VM, "I'm done with what I am doing right now; wake me up in a little while." By putting a thread to sleep, you are allowing lower-priority threads a chance to get a shot at the processor. This is especially important when very low-priority threads are doing tasks that, although not as important, still need to be done periodically. Without stepping out of the way occasionally, your thread can put these threads into starvation . As stated earlier, this is where one or more threads never get a chance to execute because other threads are using the CPU too much and won't give it up.

The sleep method comes in two varieties. The first is sleep(long), which tells the interpreter that you want to go to sleep for a certain number of milliseconds :

 thisThread.sleep(100); 

The only problem with this version is that a millisecond, although only an instant for humans , is a long time for a computer. Even on a 486/33 computer, this is enough time for the processor to do 25,000 instructions. On high-end workstations, hundreds of thousands of instructions can be done in 1 millisecond.

As a result, there is a second incantation: sleep(long,int). With this version of the sleep command, you can put a thread to sleep for a number of milliseconds, plus a few nanoseconds:

 thisThread.sleep(99,250); 

Note

You might notice something strange if you look at the Sun API's for the sleep methods. Notice that these two methods are static. That means that you should be able to do something like this: Thread.sleep(3000). If you call sleep on a thread instance object, only the current thread will sleep. You can't tell an outside thread to sleep from a different thread.


The yield method is very similar to the sleep method. With yield, you're telling the interpreter that you want to get out of the way of the other threads, but when they are done, you want to pick back up. yield does not require a resume to start back up when the other threads have stopped , gone to sleep, or died.

Caution

You may see older Java code that uses the suspend or the resume method when dealing with threads. Both of these methods have been deprecated by Sun and can be very dangerous to use.


The last method to change a thread's running state is the destroy method. In general, don't use this method. The destroy method doesn't do any cleanup on the thread; it just destroys it. However, if you read the Sun API for this method, you will see that they say this method is not implemented. So, you really should never try to use this method anyway.

   


Special Edition Using Java 2 Standard Edition
Special Edition Using Java 2, Standard Edition (Special Edition Using...)
ISBN: 0789724685
EAN: 2147483647
Year: 1999
Pages: 353

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net