-1

I am learning threads right now in school, and we had to write a program that used multiple threads, however, it did not work properly until I used thread.join()

It works like it should now, but I am not entirely sure what is happening.

Originally I had something like this. It caused the output of the threads to conflict with one another.

t1.start();
t2.start();
t3.start();

I then did this, and the output was fine and the correct answer was achieved.

 t1.start();
 t1.join();
 t2.start();
 t2.join();
 t3.start();
 t3.join();

My question is, what is happening in the first example compared to the second? I've been googling and searching on stackoverflow, but can not seem to find an answer that completely helps me understand.

sbowde4
  • 767
  • 1
  • 4
  • 23
  • If your program wasn't working before the calls to `.join()` then you most likely were running into a synchronization issue (without the entire code it is hard to tell). If you are sharing and modifying the same Object(s) across the 3 threads you may want to look into using some sort of synchronization – Jayfray Feb 16 '17 at 20:26
  • That is exactly what I am looking into now, I stumbled across a post about. I am sharing a 2D array across threads. – sbowde4 Feb 16 '17 at 20:28
  • 1
    You will most likely want to look at the `synchronized` keyword or the [ReentrantLock](http://docs.oracle.com/javase/8/docs/api/java/util/concurrent/locks/ReentrantLock.html) – Jayfray Feb 16 '17 at 20:40
  • Jayfray I wish I could mark your comment correct! `synchronized` fixed the issue! Thank you. – sbowde4 Feb 16 '17 at 20:52
  • Technically the `synchronized` isn't the answer to your question. You asked what the difference was between using `join()` and not using it. Although I read between the lines and knew what your problem was. Glad I could help. – Jayfray Feb 16 '17 at 20:57

3 Answers3

4

t.join() causes the current thread to pause execution until t's thread terminates.

By doing this:

 t1.start();
 t1.join();
 t2.start();
 t2.join();
 t3.start();
 t3.join();

there is no reason to use threads...

thead.join() use example:

Lets say you need to encrypt 3 files, you want to use threads for faster processing time, and you want to know how much time it took:

int startTime = System.currentTimeMillis();
firstFileEncryptorThread.start();
secondFileEncryptorThread.start();
thirdFileEncryptorThread.start();

firstFileEncryptorThread.join();
secondFileEncryptorThread.join();
thirdFileEncryptorThread.join();

System.out.println(System.currentTimeMillis() - startTime );
  • 1
    To elaborate, since you're always waiting for the previous thread to finish before you start the next thread, you never have more than 1 thread running at once, which pretty much entirely defeats the purpose of using threads. – Carcigenicate Feb 16 '17 at 20:22
  • I see, I saw a post talking about thread synchronization, but it got deleted as I was commenting on it. My threads are all performing operations on the same 2D array. So Instead of doing thread.join(), I am going to look into thread synchronization – sbowde4 Feb 16 '17 at 20:24
  • "there is no reason to use threads..." so the `join()` method was introduced by error ? :-) In fact If a thread waits for input the result from another thread termination, you have to use `join()` – davidxxx Feb 16 '17 at 20:25
  • @davidxxx of corse, but in the way he used it he didn't even need threads. its the same as doing this: `methodA(); methodB(); methodC();` – אביב פישר Feb 16 '17 at 20:29
  • @davidxxx You would have `join` in a case like that, but if you're just sequencing computations, I can't see any advantage to using threads in the first place. If you wanted the computations to run parallel to the main thread, you might as well just have 1 spawned thread that you run it on instead of spawning many and "chaining" them. – Carcigenicate Feb 16 '17 at 20:30
  • @Carcigenicate @אביב פישר I agree of course.This example doesn't show a real interest to use `join()`. It is just a pity that you don't give an example where join() could be useful as it the op question. – davidxxx Feb 16 '17 at 20:42
  • @davidxxx He didn't ask for an example... but an example is a good idea to explain this. – אביב פישר Feb 16 '17 at 21:01
0

As a javadoc:

Waits for this thread to die. An invocation of this method behaves in exactly the same way as the invocation

So when you call t1.join(); the thread in wich you've make the call wait to t1 to finish before executing the next line. If t1 never finish, the next line will never be executed.

JFPicard
  • 5,029
  • 3
  • 19
  • 43
0

The second example what you did is actually similar to single thread - meaning your run a thread , wait for it to finish and than run the next thread.

public final void join(): This java thread join method puts the current thread on wait until the thread on which it’s called is dead. If the thread is interrupted, it throws InterruptedException.

In the first example - all thread are running in parallel

Can see similar question here

Community
  • 1
  • 1
Mzf
  • 5,210
  • 2
  • 24
  • 37