2:23 am - Friday April 19, 2019

Thread Join Java Example Program | Java Thread

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

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

Filed in: Z-A All Java Codes

Comments are closed.