本文实例讲述了Java通过在主循环中判断Boolean来停止线程的方法。分享给大家供大家参考,具体如下:
package Threads;
/**
* Created by Frank
*/
public class StopBoolean extends Thread {
// 确保变化对其它线程可见(主要是主线程要可见)
protected volatile boolean done = false;
public void run() {
while (!done) {
System.out.println("StopBoolean running");
try {
sleep(720);
} catch (InterruptedException e) {
return;
}
}
System.out.println("StopBoolean finished");
}
public void shutDown() {
done = true;
}
public static void main(String[] args) throws InterruptedException {
StopBoolean t1 = new StopBoolean();
t1.start();
Thread.sleep(1000 * 5);
t1.shutDown();
}
}
- 看不过瘾?点击下面链接!
- nodejs个人博客分配数据开发教程
- Java数据结构之队列
- SSD移动硬盘和USB3.1的速度一样快吗
- 微星A320手雷主板怎么样?搭配AMD Ryzen5 1400
- 旧电脑怎么升级?旧电脑哪些硬件可以升级?
- AppleMobileDeviceService.exe是什么进程?