my dog learned polymorphism
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes join method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "join method " Watch "join method " New topic

join method

sweety sinha
Ranch Hand

Joined: Jul 07, 2008
Posts: 76
this is from: SCJP 6 Mock exam for Threads � Niko�s java blog.mht

if line 1 is commented then answer is 0, otherwise it is 999999.
what join() is doing here? i know that join method of thread lets one thread to join onto the end of another thread. Here when start is called on Job instance, then run method is invoked and after completion of that method thread.join() should be called. how this code is working?
Ankit Garg

Joined: Aug 03, 2008
Posts: 9466

The join method works in this fashion

in a thread(let's name it t1) you create a thread instance(let's name it t2). then you start that thread t2. then in t1, if you want something to be executed only when t2, then you call t2.join. The call will only return when t2 completes execution. So the execution of t1 will stop or pause till t2 completes.

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
chander shivdasani
Ranch Hand

Joined: Oct 09, 2007
Posts: 206

What is happening is,
Thread.join() method joins the current thread(Main in this case) with the previously started thread.

So what happens is, Main method waits for the previously started thread to complete. After the thread completes, main method continues to execute.

When you comment Thread.join(), what happens is, two parallel thread starts running(Main thread and the thread printing numbers in this case) and they execute without waiting for each other.

Enjoy, Chander
SCJP 5, Oracle Certified PL/SQL Developer
Mohammad Khan
Ranch Hand

Joined: Sep 23, 2008
Posts: 37
Following example might help to understand the issue you are having.

class Job implements Runnable {
private int number = 0;

public void run() {
for (int i = 0; i < 1000000; i++) {
synchronized (this) {

public int getNumber() {
return number;

public class Test {
public static void main(String[] args) throws Exception {
Job job = new Job();

Thread t1 = new Thread(job, "t1");
Thread t2 = new Thread(job, "t2");
Thread t3 = new Thread(job, "t3");

t1.start();// at this point we have main thread and t1
t2.start();// so far we have main thread, t1 and t2
t3.start();// now we have total 4 threads which are main, t1, t2 and t3

// join/append Thread t1 at the end of main thread
// this would leave us total 3 thread main, t2 and t3

// join/append Thread t2 at the end of main thread
// and this would leave main thread and t3

// join/append Thread t3 at the end of main thread
// this would leave only main thread

// making sure to print the number after finished three threads t1, t2, t3
while (true) {
if ((t1.getState() == Thread.State.TERMINATED) && (t2.getState() == Thread.State.TERMINATED) && (t3.getState() == Thread.State.TERMINATED)) {
// If this.number incrementation is not synchronized in run method,
// it is not guaranteed to satisfy number == 3000000
I agree. Here's the link:
subject: join method
It's not a secret anymore!