interrupt 函数
如何中断线程?
已经被抛弃的方法:
一、通过调用 stop()方法停止线程
stop 太过于暴力且不安全的,使得被停止的线程来不及清理,会马上释放锁,引发数据不同步的问题
二、suspend() 方法 resume ()方法
目前使用的方法:
一、interrupt (),通知线程应该中断了
- public class InterruptDemo {
- public static void main(String[] args) throws InterruptedException {
- Runnable interruptTask = new Runnable() {
- @Override
- public void run() {
- int i = 0;
- try {
- //在正常运行任务时,经常检查本线程的中断标志位,如果被设置了中断标志就自行停止线程
- while (!Thread.currentThread().isInterrupted()) {
- Thread.sleep(100); // 休眠100ms
- i++;
- System.out.println(Thread.currentThread().getName() + " (" + Thread.currentThread().getState() + ") loop " + i);
- }
- } catch (InterruptedException e) {
- //在调用阻塞方法时正确处理InterruptedException异常。(例如,catch异常后就结束线程。)
- System.out.println(Thread.currentThread().getName() + " (" + Thread.currentThread().getState() + ") catch InterruptedException.");
- }
- }
- };
- Thread t1 = new Thread(interruptTask, "t1");
- System.out.println(t1.getName() +" ("+t1.getState()+") is new.");
- t1.start(); // 启动“线程t1”
- System.out.println(t1.getName() +" ("+t1.getState()+") is started.");
- // 主线程休眠300ms,然后主线程给t1发“中断”指令。
- Thread.sleep(300);
- t1.interrupt();
- System.out.println(t1.getName() +" ("+t1.getState()+") is interrupted.");
- // 主线程休眠300ms,然后查看t1的状态。
- Thread.sleep(300);
- System.out.println(t1.getName() +" ("+t1.getState()+") is interrupted now.");
- }
- }