BOSS
BOSS

Reputation: 2951

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 ?

Upvotes: 24

Views: 56013

Answers (10)

Kishor Bhoyar
Kishor Bhoyar

Reputation: 121

All the parallel threads typically needs to join at some point in the code thereby allowing them to wait until all threads terminate. After this point typically the serial processing continues. This ensures proper synchronisation between peer threads so that subsequent serial code does not begin abruptly before all parallel threads complete the collective activity.

Upvotes: 0

user207421
user207421

Reputation: 310957

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.

Upvotes: 28

Alexander Razmakhnin
Alexander Razmakhnin

Reputation: 163

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.

Upvotes: 9

Real Red.
Real Red.

Reputation: 5039

If T1 and T2 do different tasks which do not depend on state changes caused by each other - you should not join them to reap advantages of parallel execution. In case there are state dependenices you should synchronize both threads using mechanisms like wait/notify or even .Join() depending on your use case.

And as for, combining the run() methods of both threads, it's entirely left to you. I mean, you should understand why both threads are of different "types" (as they have different run() body) in the first place . It's a design aspect and not a performance aspect.

Upvotes: 0

Deepak Bala
Deepak Bala

Reputation: 11185

using Join also like we can add the T2 thread's code inside T1 thread

join() like the method name implies waits for the thread to die and joins it at the end of execution. You can add one thread's code inside another but that would destroy the purpose of using 2 separate threads to run your jobs concurrently. Placing one code after the other would run your statements in sequence. There is no concurrency.

When in doubt, consult the javadocs - http://download.oracle.com/javase/1.5.0/docs/api/java/lang/Thread.html#join%28%29

Upvotes: 0

SJuan76
SJuan76

Reputation: 24885

Two things.

Join is used only when one thread must wait for the open to finish (lets say thread A prepares a file and thread B cannot continue until the file is ready). There are instance where threads are independent and no join is needed (for example most of daemon threads).

With threading you get several things: - mainly, independence in the order of execution. Lets say that you have a program that when you push a button does some heavy processing. If you do that processing in the main thread, you GUI will freeze until the task is finished. If you do the processing in another thread, then the GUI thread is "freed" and the GUI keeps working. - in some (most) of modern computers, creating several threads could allow the OS to use the different cores to serve different threads, improving performance.

The drawback is bigger complexity, as you need information of other threads execution state.

Upvotes: 2

Bohemian
Bohemian

Reputation: 425073

You could use something like a java.util.concurrent.CountDownLatch, eg:

CountDownLatch doneSignal = new CountDownLatch(2);

and have each thread countDown() when they're done, so a main thread knows when both threads have completed.

Upvotes: 1

Thomas
Thomas

Reputation: 88707

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

Upvotes: 32

Leonard Brünings
Leonard Brünings

Reputation: 13242

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.

Upvotes: 16

BOSS
BOSS

Reputation: 2951

The main difference is when we join T2 thread with T1 ,the time T2 is executing the job can be utilised by T1 also ,that means they will do different job parllely.But this cann't happen when you include the T2 thread code inside T1 thread.

Upvotes: -8

Related Questions