Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why was the method java.lang.Thread.join() named like that?

Does anybody know why the method join() member of a java.lang.Thread was named like that? Its javadoc is:

Waits for this thread to die.

When join is called on some thread calling thread is waiting for the other to die and continue execution. Supposedly calling thread will die as well, but still it's not clear why the author used this name.

like image 862
Boris Pavlović Avatar asked Jun 22 '09 11:06

Boris Pavlović


People also ask

What is the purpose of Join () method in thread class?

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.

What does join () do in Java?

Join method in Java allows one thread to wait until another thread completes its execution. In simpler words, it means it waits for the other thread to die. It has a void type and throws InterruptedException.

What is thread join () in threading?

Join() Blocks the calling thread until the thread represented by this instance terminates, while continuing to perform standard COM and SendMessage pumping. Join(Int32)

Is thread join necessary?

No join or System. exit necessary. Each thread lives its own life. As long as at least one thread is running, the program keeps running.


3 Answers

It's a common name in threading - it's not like Java was the first to use it. (For example, that's what pthreads uses too.)

I guess you could imagine it like two people taking a walk - you join the other one and walk with them until you've finished, before going back to what you were doing. That sort of analogy may have been the original reason, although I agree it's not exactly intuitive.

like image 200
Jon Skeet Avatar answered Oct 22 '22 01:10

Jon Skeet


It's named this way because you're basically stating that the calling thread of execution is going to wait to join the given state of execution. It's also named join in posix and many other threading packages.

After that call to join returns (unless it was interrupted), the two threads of execution are basically running together from that point (with that thread getting the return value of the now-terminated thread).

like image 31
Jason Coco Avatar answered Oct 22 '22 03:10

Jason Coco


This stems from concurrent software modeling when the flow of control splits into to concurrent threads. Later, the two threads of execution will join again.

Also waitToDie() was probably a) too long and b) too morbid.

like image 22
Aaron Digulla Avatar answered Oct 22 '22 02:10

Aaron Digulla