When running thread suddenly calls the sleep() method then immediately it will enter into sleeping state. sleep() method is called as
the thread will come out from sleeping state to ready state if time expires or if sleeping thread got interrupted. sleep() method by default throws an exception, so we have to call sleep() method within a try and catch block. Let's see an example for better clarity:
class SleepDemo extends Thread { public void run() { for(int i=1;i<5;i++) { try { Thread.sleep(500); } catch(InterruptedException e) { System.out.println(e); } System.out.println(i); } } public static void main(String args[]) { SleepDemo sd1=new SleepDemo(); SleepDemo sd2=new SleepDemo(); sd1.start(); sd2.start(); } }
1
1
2
2
3
3
4
4
Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.
We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc