TODAY JAVA SCHOOL

In java school, programming, design, computer general knowledge, web application, software, web services, social media, digital marketing, oops, concept of programming language, oops feature, console media, graphics medium, first programming, c, c ++ , Java, PHP, SQL, MySQL, HTML, HTML_5, J_query, JavaScript, Bootstrap, Framework, images with logos, examples, shared and explained.

https://www.amazon.in/b?node=26373545031&linkCode=ll2&tag=1234567801cdb-21&linkId=3b9882431b00409b44141e0344b35a15&language=en_IN&ref_=as_li_ss_tl

Breaking

Wednesday, 15 April 2020

Sleep Method In Java | Wait And Sleep Method In Java | Wait And yield Method In Java

Sleep method in java

The sleep() method of Thread class is used to sleep a thread for the specified amount of time.

wait and sleep method in java


Fig: Sleep Method In Java 


Syntax of sleep() method in java

The Thread class provides two methods for sleeping a thread:
  • public static void sleep(long miliseconds)throws InterruptedException
  • public static void sleep(long miliseconds, int nanos)throws InterruptedException

Example of sleep method in java

    class TestSleepMethod1 extends Thread{  
     public void run(){  
     for(int i=1;i<5;i++){ 
 try{Thread.sleep(500);}catch(InterruptedException e){System.out.printl        n(e);}  
    System.out.println(i);  
   }  
  }  
   public static void main(String args[]){  
   TestSleepMethod1 t1=new TestSleepMethod1();  
   TestSleepMethod1 t2=new TestSleepMethod1();  
      
  t1.start();  
  t2.start();  
    }  
  }  

   

   Output:

       1
       1
       2
       2
       3
       3
       4
       4

No comments:

Post a Comment