Join Method in Thread.
Java Thread use of join method . Two thread ThreadA and ThreadB taken
then one by one join the execution
package Thrad;
public class ThreadA extends Thread {
public void run() {
System.out.println("threadA exit");
for(int i=1;i<=5;i++) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("::threadA::");
}
}
}
package Thrad;
public class ThreadB extends Thread{
public void run() {
System.out.println("threadB exit");
for(int i=1;i<=5;i++) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("::threadB::");
}
}
}
package Thrad;
import java.io.*;
import java.lang.*;
class ThreadJoin
{
public void main(String args[])
{
ThreadA ta=new ThreadA();
ta.start();
Thread tb=new Thread();
tb.start();
try {
ta.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
try {
tb.join(); //Join method here
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("both threads executed ");
for(int i=1;i<=5;i++) {
try {
Thread.sleep(200);
}
catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("::mainthread::");
}
System.out.println("mainthread exit");
}
}
Join Method in Thread.
Reviewed by Mukesh Jha
on
8:48 AM
Rating:

No comments:
Add your comment