capint
2/3/2017 - 5:18 PM

Java >> Thread >> Examples

Java >> Thread >> Examples

// Source: http://docs.oracle.com/javase/tutorial/essential/concurrency/simple.html
long startTime = System.currentTimeMillis();
Thread t = new Thread(new MessageLoop());
t.start();

threadMessage("Waiting for MessageLoop thread to finish");
// loop until MessageLoop
// thread exits
while (t.isAlive()) {
    threadMessage("Still waiting...");
    // Wait maximum of 1 second
    // for MessageLoop thread
    // to finish.
    t.join(1000);
    if (((System.currentTimeMillis() - startTime) > patience)
          && t.isAlive()) {
        threadMessage("Tired of waiting!");
        t.interrupt();
        // Shouldn't be long now
        // -- wait indefinitely
        t.join();
    }
}