Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

Thread Join Java Example Program | Java Thread

Join() method helps us to Join one Thread to the end of another thread. Here is a simple java thread example Program.

// Thread Example Program for join()
/**
@author: candidjava.com
@description: The program waits until the thread dies
*/

class Sample implements Runnable {
public void run() {
try {
int j;
for (j = 0; j & lt; = 10; j++) {
System.out.println(j);
Thread.sleep(1500);
}
} catch (InterruptedException ie) {}
}

public static void main(String args[]) {
try //Try block for InterruptedException crated by join()
{
Sample s1 = new Sample();
Thread t1 = new Thread(s1); //Thread object t1 is created
t1.start(); //The thread starts the run method
t1.join(); //The program waits until the thread dies
} catch (InterruptedException ie) {}
for (int i = 11; i & lt; = 20; i++) {
System.out.println(i);
}
}
}

Output:

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20



This post first appeared on Core Java,Servlet,Jsp,Struts,Hibernate,Spring Fram, please read the originial post: here

Share the post

Thread Join Java Example Program | Java Thread

×

Subscribe to Core Java,servlet,jsp,struts,hibernate,spring Fram

Get updates delivered right to your inbox!

Thank you for your subscription

×