Signup/Sign In

Java Sleeping Thread

To sleep a thread for a specified time, Java provides sleep method which is defined in Thread class. The sleep method is an overloaded method which are given below. It throws interrupted exception so make sure to provide proper handler.

It always pause the current thread execution. Any other thread can interrupt the current thread in sleep, in that case InterruptedException is thrown.

Syntax

sleep(long millis)throws InterruptedException
sleep(long millis, int nanos)throws InterruptedException

Example : Sleeping a thread

In this example, we are sleeping threads by using sleep method. Each thread will sleep for 1500 milliseconds and then resume its execution. See the below example.

    
public class MyThread extends Thread
{ 
        MyThread(String str){
            super(str);
        }
    
        public void run()
        {
                System.out.println(Thread.currentThread().getName()+" Started");
                try{
                    MyThread.sleep(1500); 
                    }catch(InterruptedException ie){
                        System.out.println(ie);
                }
                System.out.println(Thread.currentThread().getName()+" Finished");
        }
        public static void main(String[] args)
        {
                MyThread t1=new MyThread("first thread");
                MyThread t2=new MyThread("second thread");
                t1.start();
                t2.start();
        }
}
    

first thread Started second thread Started first thread Finished second thread Finished

Example

    
public class MyThread extends Thread
{ 
        MyThread(String str){
            super(str);
        }
    
        public void run()
        {
                System.out.println(Thread.currentThread().getName()+" Started");
                try{
                    MyThread.sleep(1500);
                    System.out.println(Thread.currentThread().getName()+" Sleeping..");
                    }catch(InterruptedException ie){
                        System.out.println(ie);
                }
                System.out.println(Thread.currentThread().getName()+" Finished");
        }
        public static void main(String[] args)
        {
                MyThread t1=new MyThread("first thread");
                MyThread t2=new MyThread("second thread");
                System.out.println(t1.getName()+" state: "+t1.getState());
                t1.start();
                System.out.println(t1.getName()+" state: "+t1.getState());
                System.out.println(t2.getName()+" state: "+t2.getState());
                t2.start();
                System.out.println(t2.getName()+" state: "+t2.getState());
        }
}
    

first thread state: NEW first thread state: RUNNABLE second thread state: NEW first thread Started second thread state: RUNNABLE second thread Started first thread Sleeping.. second thread Sleeping.. second thread Finished first thread Finished

Thread.sleep() interacts with the thread scheduler to put the current thread in wait state for specified period of time. Once the wait time is over, thread state is changed to runnable state and wait for the CPU for further execution. So the actual time that current thread sleep depends on the thread scheduler that is part of operating system.