Why is the word "join" is used for the Thread.join() method?
Asked Answered
H

2

7

I started to do research to find out why the word "join" is used for the Thread.join() method. In fact, it waits for the thread to end on which it is called and pauses the main thread to wait for it so, there is nothing to join. So, its name should be something like:

  1. Thread.pauseCaller();
  2. Thread.freezeCaller();
  3. Thread.focusThread();
  4. Thread.runBefore();

I found one simple sentence after too much research which says that:

Thread.join(); name comes from the concept of the calling thread waiting until the specified thread joins it.

I am totally unable to get this above sentence into my mind and failed to understand the background context of using the word join for this method. What does the word "join" represent in this context? There is nothing to join anything else; instead, it just pauses the calling thread to wait for the thread followed by the join(). So, can anyone tell the context of using the join word for this method?

Hoag answered 1/1, 2019 at 21:53 Comment(6)
"it completes the thread on which it is called" No it doesn't. It doesn't in any way affect the thread on which it is called. It simply waits for the thread to end. Which is what the javadoc says too: Waits for this thread to die.Illsorted
I agree that it's not an intuitive name. It's a good question why it has this strange name (there is a reason, but if you don't know it it's not obvious). I don't think your proposed names get the concept right, though. Something like await() would be more natural.Scupper
The heavy downvoting is a disappointing. We shouldn't be penalizing the OP for being confused. It's better to explain the question's misconceptions in answers than to downvote it because he doesn't quite understand what joining does. If the method were named better perhaps he wouldn't be so confused. After all, it's a name that comes from UNIX. Java doesn't actually have "forking". Really, "join" isn't all that great a name now is it?Scupper
@JohnKugelman: agreed, beginner questions are ok and the history of a name may be important to understanding its meaning.Darell
@JohnKugelman Sure, Java has forking. I'm referring to the original definition from the Fork–Join model, which pre-dates the Unix implementation of forking processes. Forking is a concept, e.g. thread.start() is a fork operation, and stream.parallel() may trigger fork operations.Illsorted
@John Kugelman - Thankyou so much for the encouragement. Too much respect for you that you understand me why I asked the question.Hoag
I
17

The word "join" comes from the Fork–Join model, where "fork" means to split the thread into multiple threads for parallel processing, and "join" means to wait for the parallel threads to complete their part, before continuing in a single thread.

Illsorted answered 1/1, 2019 at 22:3 Comment(1)
So basically a new thread creates a new execution path [a fork] and join then merges back the new execution path to the one of the parent thread.Carlinecarling
R
6

Join is a common term used in models of concurrency, which implies that two separate independently running threads are synchronising with each other and one thread will not proceed until the other reaches a specific join point. So 2 threads are joining back together into one. In Java, this is usually when the other Thread finishes its task.

Note that this might not be exactly the same in models where the tasks are backed by reusable worker threads, such as when using an ExecutorService to wait for a result, and using CompletableFuture.join() to block until the result is available. In this case the joining is more related to the task executed in parallel than the actual underlying worker thread which might be reused for other tasks afterwards.

The name itself comes from the fork-join model for parallelizing tasks and collecting their results.

Ry answered 1/1, 2019 at 22:5 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.