-1

I am studying the Threads in java.

I just want a simple example which explains the use of join() in Thread. I have seen this link..

Understanding join() method example

But still not able to understand the concept.

Can anybody explain me the concept of using the join() in Thread.

Any explanation retlated to this will be very helpful to me.

Thanks.

3

4 Answers 4

2

The simplest explanation I can come up is that join makes the caller thread wait for the completion of specified thread.

Say if you have a "main thread" and "thread A", if from the main thread you call A.join(), the main thread will wait until thread A finishes.

The java se manual page about concurrency should help you here: http://docs.oracle.com/javase/tutorial/essential/concurrency/join.html

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

2 Comments

but isAlive() also does the same thing right?? So why join() is provided??
Wrong. isAlive() just returns true or false to indicate whether or not a thread is alive. It does not cause the calling thread to wait.
1

Thread.join() causes the current thread to wait for the thread you call join() on to die before it resumes execution.

Comments

0

Thread.join() blocks (does not return) until the thread you joined on has finished.

This is not the only way to wait for a thread to finish, but it is the most CPU-usage friendly way. Imagine if you had a loop like this (pseudocode):

while(!thread.isAlive()) { Sleep(1); } 

This supposedly does the same thing... but, 1000 times per second, it will wake up, check the variable and go back to sleep. This means 1000 context switches (which are expensive) and the program will be slower as a result. This is called 'spinlocking' or 'busywaiting' and is frowned upon in programming as it consumes CPU for no reason.

3 Comments

If I have 3 thread objects like, t1,t2,t3 and I do t1.join();t2.join();t3.join(). So here the t1,t2,t3 gets executed at same time, t2 is not waiting for t1 to be finished, why is it so??
..because t2 is not calling join() on t1.
@MartinJames Ok you mean to say that t1 and t2 calling the join() on the main thread. Am I right??
0

I did some experiment and here is the result: 1. first started thread t3. 2. started t1 then 3. created t2 and t2 joinned the running thread t1. By definition, t2 should wait for t1 to die and then it should start.

Observation: I called wait on t1, so t1 is paused but not died but I see then t2 is started why ?

public class TestThreadJoin { public static void main(String[] args) { final Thread t3 = new Thread(new Runnable() { public void run() { System.out.println("R3"); } }); t3.start(); final Thread t1 = new Thread(new Runnable() { public void run() { System.out.println("R1 before"); try { perform(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("R1 after"); } private void perform() throws InterruptedException { synchronized(this){ wait(5000); } } }); t1.start(); Thread t2 = new Thread(new Runnable() { public void run() { System.out.println("R2"); try { t1.join(); } catch (InterruptedException e) { e.printStackTrace(); } } }); t2.start(); } } 

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.