Java join() methos example
This program give the details how thread join() method works in java.In this program we have created three threads and make it sleep so that other Thread can start and run . While the old thread sleep after the start.
class Threadmmy extends Thread
{
public void run() {
for( int i=0 ; i<3 ; i++) {
System.out.println(" Produced");
try {
Thread.sleep(400);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("Thread ended: "+ Thread.currentThread().getName());
}
}
}
class JoinMethodThread {
public static void main(String[]args) throws InterruptedException
{
Threadmmy t1 = new Threadmmy ();
Threadmmy t2 = new Threadmmy ();
Threadmmy t3 = new Threadmmy ();
// this will call run() method
// t2.setPriority(Thread.MAX_PRIORITY);
t1.start();
t2.start();
t2.join();
t1.join();
t3.start();
t3.join();
// System.out.println(Thread.currentThread().getName() + " in control");
}
}
This program will have this kind of output . You can how threads starts and ends the execution. Thread t1 sleeps and then t2 join without all loop compilations.Code out put gives all deatils of join() methid.
Produced
Produced
Thread ended: Thread-0
Produced
Thread ended: Thread-1
Produced
Thread ended: Thread-0
Produced
Thread ended: Thread-1
Produced
Thread ended: Thread-0
Thread ended: Thread-1
Produced
Thread ended: Thread-2
Produced
Thread ended: Thread-2
Produced
Thread ended: Thread-2
Java join() methos example
Reviewed by Mukesh Jha
on
4:44 AM
Rating:
No comments:
Add your comment