0

Say i have two threads t1 and t2, as per Oracle docs, t1.join means the current thread will wait for t1 to finish.My question is, what if the threads have already finished ? eg:

Thread t1 = new Thread(new EventThread("e1"));
t1.start();
Thread e2 = new Thread(new EventThread("e2"));
t2.start();
while (true)
{
    try {
    t1.join();
    t2.join();
    break;
    }
}

What if t2 is already finished ?

Parameswar
  • 1,951
  • 9
  • 34
  • 57
  • 2
    What are the options? Have you done any testing? Have you read the [documentation](https://docs.oracle.com/javase/7/docs/api/java/lang/Thread.html#join(long))? – shmosel Mar 29 '17 at 18:35

1 Answers1

0

From the documentation of join():

An invocation of this method behaves in exactly the same way as the invocation

join(0)

And the documentation of join(long millis) says:

This implementation uses a loop of this.wait calls conditioned on this.isAlive.

So if isAlive returns false (i.e. the thread finished), the wait is never called and join just returns immediately.

Community
  • 1
  • 1
M A
  • 71,713
  • 13
  • 134
  • 174