Friday 5 July 2019

Prevent a Thread : Sleep

  • If a Thread don't want to perform any operation for a particular amount of time then we should go for sleep() method.
Prototypes of join():
  • public static native void sleep(long ms) throws InterruptedException
  • public static void sleep(long ms,int ns)throws InterruptedException 
 Join Life cycle:-



Example: 

class ThreadJoinDemo
{
            public static void main(String[] args)throws InterruptedException
            {
                     System.out.println("P");
                     Thread.sleep(3000);
                     System.out.println("K");
                     Thread.sleep(3000);
                     System.out.println("N");
            }
}

Output:
P
K
N


Thanks..!!

No comments:

Post a Comment