简体   繁体   中英

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM