Java Thread Sleep Example


Sleep method of java.lang.Thread is used to pause current execution of thread for specific period of time.

Some important points about sleep method are :

  • It causes current executing thread to sleep for specific amount of time.
  • Its accuracy depends on system timers and schedulers.
  • It keeps the monitors it has acquired, so if it is called from synchronized context, no other thread can enter that block or method.
  • If we call interrupt() method , it will wake up the sleeping thread.

Example: Create a class as below.

Create main class named

When you run above program, you will get following output.

You can see there is delay of 1 milliseconds. As mentioned earlier,its accuracy depends on system timers and schedulers.

You may also like:


Add Comment