`
D-tune
  • 浏览: 76655 次
  • 性别: Icon_minigender_1
  • 来自: 上海浦东
文章分类
社区版块
存档分类
最新评论

Determining When a Thread Has Finished

阅读更多
// 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
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics