Java Sleep method


The sleep() method is used to sleeps a thread for the specified number of milliseconds.

Syntax:

static void sleep(long millisecs) 
throws InterruptedException 

static void sleep(long millisecs, int 
nanosecs) throws InterruptedException 

Example

class SleepMethod extends Thread{  
    public void run(){  
        try{
            for(int c=1; c<5; c++){ 
                System.out.println(c);
                Thread.sleep(200);
            }   
        }catch(InterruptedException e){
            System.out.println(e);
        }  
    }  
}
public class SleepMethodDemo
 {
    public static void main(String args[]){  
        SleepMethod t=new SleepMethod();  
        SleepMethod t1=new SleepMethod();  
        t.start();  
        t1.start();  
    }  
}  



OnlineTpoint is a website that is meant to offer basic knowledge, practice and learning materials. Though all the examples have been tested and verified, we cannot ensure the correctness or completeness of all the information on our website. All contents published on this website are subject to copyright and are owned by OnlineTpoint. By using this website, you agree that you have read and understood our Terms of Use, Cookie Policy and Privacy Policy.