Java安全停止线程方法
来源:互联网 发布:java语言培训机构 编辑:程序博客网 时间:2024/06/07 15:35
1.早期Java提供java.lang.Thread类型包含了一些列的方法start(), stop(), stop(Throwable) and suspend(), destroy() and resume()。,Sun公司的一篇文章《Why are Thread.stop, Thread.suspend and Thread.resume Deprecated? 》
2.使用volatile变量来设置Thread的run的循环条件
- public class JavaTest extends Thread{
- private volatile boolean isRun = true;
- public static void main(String[] args) {
- JavaTest thread = new JavaTest();
- thread.start();
- thread.close();
- }
- @Override
- public void run() {
- while (isRun) {
- //dosomething
- }
- }
- public void close() {
- this.isRun = false;
- }
- }
- public class JavaTest extends Thread{
- private volatile boolean isRun = true;
- public static void main(String[] args) {
- JavaTest thread = new JavaTest();
- thread.start();
- thread.close();
- if (thread != null) {
- thread.interrupt(); //外围调用关闭
- }
- }
- @Override
- public void run() {
- while (isRun) {
- //dosomething
- try {
- wait(); //同样适用于sleep等状态
- } catch (InterruptedException e) {
- //catch Exception
- }
- }
- }
- public void close() {
- this.isRun = false;
- }
- }
- Java安全停止线程方法
- Java安全停止线程方法
- Java线程停止方法
- Java停止线程及有锁时停止方法
- java停止线程的方法
- java 线程停止的方法
- Java线程停止的方法
- Java线程停止的方法
- 线程池/安全停止线程
- 停止Java线程,小心interrupt()方法
- 论停止Java线程的方法
- java/android 线程停止的方法
- 停止Java线程,小心interrupt()方法
- 停止Java线程,小心interrupt()方法
- Java中停止线程执行的方法
- 停止Java线程,小心interrupt()方法
- 停止Java线程,小心interrupt()方法
- Java中停止线程执行的方法
- clang的编译和使用
- mysql 重定义分隔符
- 新手学Java 7编程(第5版)
- eclipse开发hadoop hdfs api 运行错误
- extjs 动态表格
- Java安全停止线程方法
- 线程同步-event机制
- 远程cp文件命令
- 无需苹果审核的App发布——企业版IDP的申请及“In House”发布
- 查某人未阅
- 年过50,还能继续当码农吗?
- seo之网站地图
- RPC编程
- MapABC地图使用