shutdownNow关不掉线程
2015-12-07 15:01
197 查看
@Override public void run() { try { while (!Thread.interrupted()) { while (!car.waxOn) { car.waxOn(); } } } catch (InterruptedException e) { System.out.println("waxon interrupted"); } } @Override public void run() { while (!Thread.interrupted()) { while (!car.waxOn) { try { car.waxOn(); } catch (InterruptedException e) { System.out.println("waxon interrupted"); } } } }
上下两个run方法比较,while (!Thread.interrupted()) 判断应该放在try代码快里。否则像下面的写法,catch会吞掉InterruptedException,到时判断检测不出来以至于shutdownNow关不掉线程
shutdownNow的原理是为每个线程调用 interruput方法,然后有run方法里的判断语句来检查Thread.interrupted()
相关文章推荐
- C#线程间不能调用剪切板的解决方法
- C#线程同步的三类情景分析
- C#获取进程或线程相关信息的方法
- C#停止线程的方法
- C#子线程更新UI控件的方法实例总结
- C#线程队列用法实例分析
- C++使用CriticalSection实现线程同步实例
- 基于C++实现的线程休眠代码
- VB读取线程、句柄及写入内存的API代码实例
- C#网络编程基础之进程和线程详解
- C#通过Semaphore类控制线程队列的方法
- C#多线程处理多个队列数据的方法
- C#实现线程安全的简易日志记录方法
- C#中线程同步对象的方法分析
- ASP.NET线程相关配置
- 浅析linux环境下一个进程最多能有多少个线程
- 再谈JavaScript线程
- C#实现终止正在执行的线程
- Java线程编程中的主线程讲解
- 解析Java线程同步锁的选择方法