2

I was researching on Thread join() method and i came across the ThreadJoinMethod post on stackoverflow. I modified the code to develop a working example and i am confused on the output. Code snippet is.

class JoinRunnable implements Runnable{ public void run() { for(int i =0 ; i < 4 ; i++){ System.out.println(i); } } } public class TestJoin{ public static void main(String[] args) throws InterruptedException { JoinRunnable joinRunnable = new JoinRunnable(); Thread t1 = new Thread(joinRunnable); Thread t2 = new Thread(joinRunnable); t1.start(); t2.start(); System.out.println("Currently running thread: " + Thread.currentThread().getName()); t1.join(); t2.join(); System.out.println("I must wait"); } } 

The output of the following program is :-

0 1 2 3 0 1 2 3 Currently running thread: main I must wait 

I am confused at the output. The current thread will be joined after the call to join on t1 and t2 but why the statement, "Currently Running Thread: main" is printing after t1 and t2 completes? Am i missing some important concept here? Because main() will join t1 and t2 after the join statements not before. Can someone elaborate on it?

3
  • Is it possible that the other two threads simply finish their procedures before the calls to the various libraries involved in Thread, System, out etc, yields a result? Test, by making it output 1 to 10000 for each of them.. then have fun sifting through. Commented Aug 7, 2013 at 13:49
  • 2
    Add a sleep in the loop iteration, and you will see that you won't get the same result. Commented Aug 7, 2013 at 13:52
  • perfect, i just added sleep now Currently running method-main is printed first. So output must differ. Thanks a ton. Commented Aug 7, 2013 at 13:55

6 Answers 6

4

The thread starts after you call start() This means it can start before any line which comes after and can even complete before the lines which come after it.

Sign up to request clarification or add additional context in comments.

Comments

2

You are interpreting it wrong way. join() method causes the main thread to wait for t1 and t2. So that main won't exit before completion of t1 and t2. And it is completely arbitrary what is printed first, mainly depending on priority of threads. To set a predefined behavior, you have to synchronize the threads.

5 Comments

But it is a possibility that t1 and t2 dont come into running state and this statement prints "Currnetly running thread main". Is it?
@RohitJain Read again, he is saying that main thread will be joined(means it will wait).
@user2550754. Yeah that part is ok. What OP is asking is why Currently running thread: main, which is printed before both the join()s prints at the end of both threads?
@benz Actually, what happens main thread will continue its execution until it comes across t1.join() and t2.join(). Now from here, main or current thread will wait for both t1 and t2 to finish execution. It is completely arbitrary what is printed first, mainly depending on priority of threads.
thanks @user2550754 i have understood the concept now. I added sleep for statement and now the behavior was different. Thank you so much.
2

System.out.println is synchronized on the underlying OutputStream instance (at least in the implementation I have here on my PC).

The time gaps between each println is very short, so it is very unlikely that a println from another thread will have the chance to be be executed.

So it looks like you have a certain order on the println (at least with a very high probability). Insert some sleeps and let the loops count higher and you should be able to observe a different behavior.

1 Comment

Thankyou very much people. I understood the concept here.
1

This is just running too fast. Insert a short sleep in those two threads.

Comments

1

Here is the description of the join() method :
Blocks the current Thread (Thread.currentThread()) until the receiver finishes its execution and dies. (from javadoc)
Indeed, you may block your main thread which is waiting for t1 & t2 to stop.

3 Comments

But it is a possibility that t1 and t2 dont come into running state and this statement prints "Currnetly running thread main". Is it?
I think Peter Lawrey comment's is more relevant for your problem than mine, I think you should add some short sleep (as said above) to see if the process of t1 & t2 finishes before or not.
Thanks @Sw4Tish. I did that and output was different this time.
1

Thread t1 and t2 started and finished their work before your statement

 System.out.println("Currently running thread: " + Thread.currentThread().getName()); 

can actually produce any output on screen.So the threads t1 and t2 were have completed their task and the main thread can move on as threads joined to that are done with their run() method.

If you want to learn and understand concept of Joining two threads, kindly provide Thread.sleep(1000) in your for loop in run method.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.