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 ...

Get Special Edition Using Java 2 Standard Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.