Rollerball
Rollerball

Reputation: 13108

Thread.join() (instance method)

Why is it not possible to .join() before starting a thread? Should not be implicit that if I call join() on a thread even if not started previously should be started?

Upvotes: 1

Views: 127

Answers (2)

stinepike
stinepike

Reputation: 54682

Acorrding to javadoc

The join method allows one thread to wait for the completion of another. If t is a Thread object whose thread is currently executing,

t.join();

causes the current thread to pause execution until t's thread terminates. Overloads of join allow the programmer to specify a waiting period.

SO it clearly tells that if you join a thread then the current thread will wait until the termination of thread. So if a thread is not started yet how can it be terminated.

Proof By Contradiction :D

say you can join a thread t without starting. then you call

t.join();

now as per the behavior of join current thread will be waiting. Now say some evil thinking (!) comes to your mind and you haven't started the thread (:D). What will be happened now? Imagine this scenario and you will find your answer

Upvotes: 2

Random42
Random42

Reputation: 9159

Because join() waits for a thread to die as the Javadoc say; it cannot die if it hasn't started yet.

I do not think it should start a thread if it is not started because the method would do two things: start a thread and wait for the thread, which is bad design.

Upvotes: 6

Related Questions