0
class JoinDemo extends Thread {
    JoinDemo(String nm) {
        setName(nm);
        start();
    }
    public void run() {
        for (int i = 1; i <= 5; i++) {
            try {
                Thread.sleep(100);
            } catch (Exception e) {
                System.out.println(e);
            }
            System.out.println(i);
        }
        System.out.println(getName() + " exiting.");
    }
    public static void main(String args[]) {
        JoinDemo t1 = new JoinDemo("One");
        JoinDemo t2 = new JoinDemo("Two");
        JoinDemo t3 = new JoinDemo("Three");

        try {
            t1.join();
        } catch (Exception e) {
            System.out.println(e);
        }
        System.out.println("Main Thread Exits now.");
    }
}

The output obtained is:

1
1
1
2
2
2
3
3
3
4
4
4
5
5
Three exiting.
One exiting.
5
Main Thread Exiting
Two exiting. 

I wrote the above program after going through various sites to understand the concept of Join(). But still i'm unable to get it.The problem I'm facing is that I have used t1.join(). So thread one should exit before three, but here thread three exits before one. And every time I run the program the output is different. As sometimes it is two exiting before one, or three before one. Shouldn't thread one exit before any other thread?? As t1.join() waits for thread one to terminate before three and one??

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
R_2293
  • 101
  • 1
  • 2
  • 12
  • 3
    The first thing to do when asking for help is make sure your code is formatted and indented readably. I've done it for you on this occasion. – T.J. Crowder Mar 27 '14 at 08:39
  • Propably t1 has already finished when reaching t1.join(). To get the effect maybe change to Thread.sleep(2000). – PeterMmm Mar 27 '14 at 08:44
  • 1
    Almost exact same question asked here http://stackoverflow.com/questions/18479771/java-multithreading-concept-and-join-method?rq=1 with a great answer. – danofa Mar 27 '14 at 08:49
  • There is nothing in your code which ensures `thread one should exit before three` Where do you have any relationship between thread one and three three? Actually there is nothing guarantee that thread one will actually start before three three finishes. – Peter Lawrey Mar 27 '14 at 09:31

4 Answers4

1

No you mistook the effect of join().

when you do a t1.join()you are just asserting that the thread t1 will be finished before continuing the program.

As you can see it's what you have,

One exiting.
5
Main Thread Exiting

One exit before the end of the main symbolized by the Main Thread Exiting.

If you want your program to finish all the thread before finishing you should do :

try {
        t1.join();
        t2.join();
        t3.join();
    } catch (Exception e) {
        System.out.println(e);
    }

If you want One to finish then 2 then 3

    JoinDemo t1 = new JoinDemo("One");
    try {
        t1.join();
    } catch (Exception e) { System.out.println(e); }

    JoinDemo t2 = new JoinDemo("Two");
    try {
        t2.join();
    } catch (Exception e) { System.out.println(e); }

    JoinDemo t3 = new JoinDemo("Three");
    try {
        t3.join();
    } catch (Exception e) { System.out.println(e); }
Clad Clad
  • 2,653
  • 1
  • 20
  • 32
  • 1
    Your last example would effectively serialize execution of those threads. Instead you might want to add an example of thread 2 joining on thread 1 and thread 3 joining on 2 but all 3 being started (almost) simultaneously. – Thomas Mar 27 '14 at 08:49
  • Okay, so is it like i don't have control over the threads finishing but have control over the main thread execution. Like I can control when my main thread finishes using join()? – R_2293 Mar 30 '14 at 05:03
0

To know exactly what join() is doing,

JoinDemo t1=new JoinDemo("One");
t1.join();
JoinDemo t2=new JoinDemo("Two");
JoinDemo t3=new JoinDemo("Three");

Just call the method after declaring t1 and see.

join() method will make the already initialized Thread to complete first.So other Threads will wait till then.

RKC
  • 1,834
  • 13
  • 13
0

t1.join() simply ensures that your main thread will block until t1 has completed. You have no control over how quickly t1 will finish compared to the other two threads.

t1, t2 and t3 are at the mercy of the thread scheduler. The only guarantee you have in your code is that t1 will finish before the main thread.

Duncan Jones
  • 67,400
  • 29
  • 193
  • 254
0

You are running 3 different threads. The priority or amount of CPU used for each thread depends on the java implementation, in some cases it's done by the OS. That's why you get a different output. Joins makes the running thread wait until the joint thread dies.

I think you want this output:

   class JoinDemo extends Thread {
    JoinDemo(String nm) {
        setName(nm);
    }
    public void run() {
        for (int i = 1; i <= 5; i++) {
            try {
                Thread.sleep(100);
            } catch (Exception e) {
                System.out.println(e);
            }
            System.out.println(i);
        }
        System.out.println(getName() + " exiting.");
    }
    public static void main(String args[]) {
        JoinDemo t1 = new JoinDemo("One");
        JoinDemo t2 = new JoinDemo("Two");
        JoinDemo t3 = new JoinDemo("Three");
        try {            
            t1.start();
            t1.join();
            t2.start();
            t2.join();
            t3.start();
            t3.join();

        } catch (Exception e) {
            System.out.println(e);
        }
        System.out.println("Main Thread Exits now.");
    }
}
PbxMan
  • 7,525
  • 1
  • 36
  • 40