Thread has many different state throughout its life such as:
Thread should be in any one state of above and it can be move from one state to another by different methods and ways.
t2.join();
t2.join(1000);
t2.join(1000,10);
here t2 is the thread object.then if t2 competes or if time expires or if waiting thread got interrupted, the thread will come out from the waiting state to ready state.
Thread.sleep(1000);
Thread.sleep(1000,10);
let's see a program using some thread methods such as yield(), sleep() method.
class X extends Thread { public void run( ) { for(int i=1;i<=4;i++) { if(i==2) yield( ); System.out.println("in thread X : i="+i); } } } class Y extends Thread { public void run( ) { for(int j=1;j<=4;j++) { System.out.println("in thread Y : j="+j); } } } class Z extends Thread { public void run( ) { for(int k=1;k<=4;k++) { System.out.println("in thread Z : k="+k); if(k==1) try { sleep(1000); } catch(Exception e) { } } } } class TestMethods { public static void main(String[] args) { X ob1=new X( ); Y ob2=new Y( ); Z ob3=new Z( ); ob1.start( ); ob2.start( ); ob3.start( ); } }
in thread Y : j=1
in thread Z : k=1
in thread X : i=1
in thread Y : j=2
in thread X : i=2
in thread Y : j=3
in thread X : i=3
in thread Y : j=4
in thread X : i=4
in thread Z : k=2
in thread Z : k=3
in thread Z : k=4
It is pointed that sleep( ) method throws an exception. In this example, sleep( ) method is enclosed with in a try block and followed by a catch block.
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