Java通过在主循环中判断Boolean来停止线程的方法示例
作者:FrankYou 时间:2022-07-18 09:15:03
本文实例讲述了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程序设计有所帮助。
标签:Java,线程
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
springboot 注解方式批量插入数据的实现
2022-02-20 19:03:50
![](https://img.aspxhome.com/file/2023/2/71692_0s.jpg)
Java springboot 配置文件与多环境配置与运行优先级
2022-08-03 03:36:01
![](https://img.aspxhome.com/file/2023/3/78903_0s.png)
Java String类字符串的理解与认知
2022-05-10 17:27:12
![](https://img.aspxhome.com/file/2023/5/60825_0s.png)
Netty分布式源码分析监听读事件
2022-10-23 20:59:43
spring mvc DispatcherServlet之前端控制器架构详解
2023-07-30 16:53:23
![](https://img.aspxhome.com/file/2023/1/71871_0s.jpg)
C#启动进程的几种常用方法
2023-06-18 04:13:48
Spring Boot 整合持久层之Spring Data JPA
2022-07-29 04:00:38
![](https://img.aspxhome.com/file/2023/9/62689_0s.png)
Mybatis-plus自定义SQL注入器查询@TableLogic逻辑删除后的数据详解
2023-04-09 22:36:45
Android编程实现向桌面添加快捷方式的方法
2023-01-24 02:10:33
![](https://img.aspxhome.com/file/2023/8/91028_0s.png)
Java ConcurrentHashMap用法案例详解
2023-08-30 02:01:41
Android常用的AlertDialog对话框及自定义对话框
2021-05-31 05:09:51
![](https://img.aspxhome.com/file/2023/9/93319_0s.gif)
java 实现线程同步的方式有哪些
2023-01-13 01:26:14
C#利用控件拖拽技术制作拼图游戏
2023-05-24 21:27:34
spring boot 集成 shiro 自定义密码验证 自定义freemarker标签根据权限渲染不同页面(推荐
2023-07-28 17:39:16
C#多线程中的互斥锁Mutex
2022-04-26 06:11:55
![](https://img.aspxhome.com/file/2023/5/69225_0s.png)
C#之继承实现
2023-04-10 16:51:41
C#抓取网络图片保存到本地的实现方法
2023-06-28 11:35:13
使用idea创建web框架和配置struts的方法详解
2022-11-14 14:21:52
![](https://img.aspxhome.com/file/2023/2/93072_0s.png)
SpringBoot项目中使用Mockito的示例代码
2023-09-25 22:31:14
![](https://img.aspxhome.com/file/2023/3/62373_0s.png)
Spring启动过程中实例化部分代码的分析之Bean的推断构造方法
2022-08-26 02:00:07