Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why we should use Join in threads?

I have 2 threads T1 and T2 ,both have different jobs so usually we prefer to accomplish this task by thread Joins.

But we can do this with out using join(). We can add T2 thread's code inside T1 thread. What difference does this make ?

like image 948
BOSS Avatar asked Jul 06 '11 09:07

BOSS


People also ask

Why do we use join in threads?

Join is a synchronization method that blocks the calling thread (that is, the thread that calls the method) until the thread whose Join method is called has completed. Use this method to ensure that a thread has been terminated. The caller will block indefinitely if the thread does not terminate.

Do we need to join threads?

Threads can be either joinable or detached. Detached threads should not be joined. On the other hand, if you didn't join the joinable thread, you app would leak some memory and some thread structures.

What happens if threads are not joined?

If you don't join these threads, you might end up using more resources than there are concurrent tasks, making it harder to measure the load. To be clear, if you don't call join , the thread will complete at some point anyway, it won't leak or anything. But this some point is non-deterministic.

What does it mean to join threads?

Joining a thread means to wait for it to complete. That is, block the current thread until another completes.


4 Answers

Joining a thread means that one waits for the other to end, so that you can safely access its result or continue after both have finished their jobs.

Example: if you start a new thread in the main thread and both do some work, you'd join the main thread on the newly created one, causing the main thread to wait for the second thread to finish. Thus you can do some work in parallel until you reach the join.

If you split a job into two parts which are executed by different threads you may get a performance improvement, if

  • the threads can run independently, i.e. if they don't rely on each other's data, otherwise you'd have to synchronize which costs performance
  • the JVM is able to execute multiple threads in parallel, i.e. you have a hyperthreading/multicore machine and the JVM utilizes that
like image 117
Thomas Avatar answered Sep 30 '22 17:09

Thomas


usually we prefer to accomplish this task by thread Joins.

No we don't. We accomplish this task by starting two threads. There is no obligation to use join() so there is no 'should' about it. If you want to pause the current thread while another thread completes, do so. If you don't, don't.

like image 35
user207421 Avatar answered Sep 30 '22 17:09

user207421


If you call T1.join(); from T2 it will wait for T1 to die (finish). It is a form of thread synchronization, but from what you describe you can simply fire of two thread and simply do not use join. If you use two threads then the work will be done in parallel, if you put the code only in one thread then the work will be done sequentially.

like image 35
Leonard Brünings Avatar answered Sep 30 '22 16:09

Leonard Brünings


Here is the reason to use join: You use it when final result depends on result of two tasks which could run at the same time.

Example1: After user clicks submit button the program has to call two external webservices to update their respective systems. It can be done at the same time that is why we would create a separate thread for one of webservices.

The user will sit before the screen and wait for a notification: Your submission is OK! The screen should say OK only after both threads finished.

like image 42
Alexander Razmakhnin Avatar answered Sep 30 '22 17:09

Alexander Razmakhnin