Home > Articles > Programming > Java

  • Print
  • + Share This
This chapter is from the book

This chapter is from the book

Thread Sleeping

Sometimes, it is necessary to pause for a certain length of time before allowing the current thread to continue its execution. For example, when generating an animation sequence, the idea is to display an image, pause, display another image, pause, and so forth. Without the pause, it is quite likely that the images that make up the animation will be displayed so fast as to make it difficult (if not impossible) to see the animation. Thread comes to the rescue with two sleep() methods. Either method pauses the current thread, when called.

Thread's sleep(long millis) method pauses the current thread for a specific number of milliseconds, as indicated by the millis parameter. In contrast, Thread's sleep(long millis, int nanos) method pauses the current thread for a specific number of milliseconds plus nanoseconds, as indicated by millis and nanos, respectively. Many platforms that implement a JVM do not support a resolution as small as a nanosecond (or even a millisecond). In such cases, the number of nanoseconds is rounded to the nearest millisecond, and the number of milliseconds is rounded to the smallest resolution supported by a platform.

The earlier ThreadDemo1 application's threads interleaved their output. By inserting a call to Thread.sleep(), the ThreadDemo4 source code in Listing 2 gives the new thread a chance to complete before the main thread enters its For loop statement.

Listing 2: ThreadDemo4.java.

// ThreadDemo4.java

class ThreadDemo4 extends Thread
{
  public void run ()
  {
   for (int i = 0; i < 20; i++)
      System.out.println ("i = " + i);
  }

  public static void main (String [] args)
  {
   ThreadDemo4 td4 = new ThreadDemo4 ();
   td4.start ();

   try
   {
     Thread.sleep (2000);
   }
   catch (InterruptedException e)
   {
   }

   for (int j = 0; j < 20; j++)
      System.out.println ("j = " + j);
  }
}

The main thread sleeps for 2000 milliseconds (2 seconds) before continuing with its execution. By that time, the new thread is finished. Although that works as indicated on a tested platform, you might want to increase the delay on your platform to observe the same result. That is due to different operating systems and the way threading works under each operating system, different processor speeds, and other factors.

NOTE

When sleep() is called, the processor puts the current thread to sleep, and runs another thread. The processor determines when the sleep time is finished, and wakes up the sleeping thread when the time expires. However, it is possible that some other thread might interrupt a thread that is sleeping. In that case, the processor prematurely wakes up the sleeping thread. In turn, the sleep() method throws an InterruptedException object instead of returning normally. To learn more about handling that kind of exception, read the Java Developer Connection TechTip Handling Those Pesky InterruptedExceptions (http://developer.java.sun.com/developer/TechTips/2000/tt0425.html#tip2).

  • + Share This
  • 🔖 Save To Your Account