Henawey
Henawey

Reputation: 558

wait executing block of code for thread dies

all i want block of code wait for another thread to die where this thread not event thread

Upvotes: 2

Views: 1268

Answers (3)

Sergei Tachenov
Sergei Tachenov

Reputation: 24879

From what I've been able to understand, it looks like you want a block of code to be executed after some thread dies, but you don't want Thread.join() to block the current thread? Then execute the whole thing in a yet another thread:

new Thread() {
  public void run() {
    someThread.join(); // someThread is the thread that you're waiting to die
    // your block of code
  }
}.start();

This will execute the code after someThread dies, but won't suspend the current thread.

Upvotes: 0

卢声远 Shengyuan Lu
卢声远 Shengyuan Lu

Reputation: 32004

java.util.concurrent.CountDownLatch can be used here.

Upvotes: 0

Grzegorz Oledzki
Grzegorz Oledzki

Reputation: 24271

I believe this is what Thread.join() method is for:

public final void join() throws InterruptedException

Waits for this thread to die.

Upvotes: 7

Related Questions