Sleeping and Waking

As shown in Listings 8.1 and 8.2, a thread can enter the waiting state by invoking its sleep() method. The sleep() method, like the yield() method, is a static method of the Thread class. It takes a millisecond time value and an optional nanosecond time value as arguments. When a thread invokes its sleep() method, it enters the waiting state. It returns to the ready state after the specified time has expired.

It is possible for another thread to awaken a sleeping thread by invoking the sleeping thread's interrupt() method. The sleeping thread then enters the ready state. When it reenters the running state, execution continues with the thread's InterruptedException handler.

Get Sun Certification Training Guide (310-025, 310-027): Java™ 2 Programmer and Developer Exams 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.