停止线程
来源:互联网 发布:raphael js 流程图 编辑:程序博客网 时间:2024/05/16 17:21
停止线程
线程的消亡不能通过调用stop()命令,而是让run()方法自然结束。stop()方法是不安全的,已经废弃。
停止线程推荐的方式:设定一个标志变量,在run()方法中是一个循环,由该标志变量控制循环是继续执行还是跳出;循环跳出,则线程结束。
如代码例子中所示:
public class ControlThreadTest{ MyThreadClass r = new MyThreadClass(); Thread t = new Thread(r); public void startThread() { t.start(); } public void stopThread() { r.stopRunning(); }}class MyThreadClass implements Runnable{ private boolean flag = true; @Override public void run() { while (flag) { System.out.println("Do something."); } } public void stopRunning() { flag = false; }}
0 0
- 停止线程
- 停止线程
- 停止线程
- 停止线程
- 停止线程
- 停止线程
- 停止线程
- 停止线程
- 线程停止
- 停止线程
- 停止线程
- 停止线程
- 停止线程
- 停止线程
- 停止线程
- 停止线程
- 线程停止
- 利用interrupt停止线程
- Android SDK no swt-pi-gtk-3550 or swt-pi-gtk in swt.library.path, java.library.path or the jar解决
- html和js实现模态框
- windows c++使用hiredis异步操作
- java 监听端口并时刻接收端口收到的信息(简单)
- python redis
- 停止线程
- 【学习笔记】查询性能优化:重构查询方式
- Oracle 创建/调用函数小示例
- Java+Ajax实现用户名重复检验
- 最短路径算法(图论)
- 半闲居士视觉SLAM十四讲笔记(3)三维空间刚体运动
- 线程池的使用 ExecutorService newFixedThreadPool(int nThreads)
- 常用小方法整理3
- 切勿用普通for循环遍历LinkedList