ITeye博客

Determining When a Thread Has Finished

D-tune · 2010-04-22 16:43 · 编程语言 · Markdown

// Create and start a thread
Thread thread = new MyThread();
thread.start();

// Check if the thread has finished in a non-blocking way
if (thread.isAlive()) {
    // Thread has not finished
} else {
    // Finished
}

// Wait for the thread to finish but don't wait longer than a
// specified time
long delayMillis = 5000; // 5 seconds
try {
    thread.join(delayMillis);

    if (thread.isAlive()) {
        // Timeout occurred; thread has not finished
    } else {
        // Finished
    }
} catch (InterruptedException e) {
    // Thread was interrupted
}

// Wait indefinitely for the thread to finish
try {
    thread.join();
    // Finished
} catch (InterruptedException e) {
    // Thread was interrupted
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。