package com.imooc.concurrent.base; public class InterruptWayStopThread extends Thread{ volatile boolean stop = false; public static void main(String[] args) { InterruptWayStopThread thread = new InterruptWayStopThread(); System.out.println("Starting thread..."); thread.start(); try { Thread.sleep(3000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("Interrupting thread..."); thread.stop = true; //线程要是处于阻塞模式,将不会检查此处的变量 thread.interrupt(); try { Thread.sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("Stopping appplication..."); } @Override public void run() { while(!stop){ System.out.println("Thread is running..."); try { Thread.sleep(1000); } catch (InterruptedException e) { System.out.println("Thread interrupted.."); } } System.out.println("Thread exiting under requestion..."); } }
哈哈,微笑不语
哎呦,这个还是用了标志位设置的。。。
thread.stop = true; //线程要是处于阻塞模式,将不会检查此处的变量
这句是什么意思啊?是字面意思吧?就是thread在sleep的时候不去取stop的值?