Java通过在主循环中判断Boolean来停止线程的方法示例
时间:2021-12-08 13:18:11|栏目:JAVA代码|点击: 次
本文实例讲述了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();
}
}
希望本文所述对大家java程序设计有所帮助。
上一篇:使用IDEA配置Mybatis-Plus框架图文详解
栏 目:JAVA代码
下一篇:如何在SpringBoot 中使用 Druid 数据库连接池
本文标题:Java通过在主循环中判断Boolean来停止线程的方法示例
本文地址:http://www.codeinn.net/misctech/186164.html


阅读排行
- 1Java Swing组件BoxLayout布局用法示例
- 2java中-jar 与nohup的对比
- 3Java邮件发送程序(可以同时发给多个地址、可以带附件)
- 4Caused by: java.lang.ClassNotFoundException: org.objectweb.asm.Type异常
- 5Java中自定义异常详解及实例代码
- 6深入理解Java中的克隆
- 7java读取excel文件的两种方法
- 8解析SpringSecurity+JWT认证流程实现
- 9spring boot里增加表单验证hibernate-validator并在freemarker模板里显示错误信息(推荐)
- 10深入解析java虚拟机




