http://android.blog.51cto.com/268543/562374
如何中断Java线程?查看API,不就是用interrupt()方法么?而线程是否已经中断则用Thread.currentThread().isInterrupted()返回true/false:
public class ThreadDemo extends Thread{ public static void main(String[] args) throws InterruptedException { Thread thread = new ThreadDemo(); thread.start(); thread.sleep(100); thread.interrupt(); //中断线程 } @Override public void run() { while(!Thread.currentThread().isInterrupted()) { System.out.println("thread running"); try { Thread.sleep(100); }catch(InterruptedException e) { System.out.println("InterruptedException"); } } System.out.println("thread interrupted"); } }
运行结果:
thread running
InterruptedException
thread running
thread running
thread running
thread running
...
很快发现这方法似乎有问题,线程没有成功中断,于是在处理InterruptedException异常后调用break跳出while循环:
public class ThreadDemo extends Thread{ public static void main(String[] args) throws InterruptedException { Thread thread = new ThreadDemo(); thread.start(); thread.sleep(100); thread.interrupt(); //中断线程 } @Override public void run() { while(!Thread.currentThread().isInterrupted()) { System.out.println("thread running"); try { Thread.sleep(100); }catch(InterruptedException e) { System.out.println("InterruptedException"); break; } } System.out.println("thread interrupted"); } }
public class ThreadDemo extends Thread{ public static void main(String[] args) throws InterruptedException { Thread thread = new ThreadDemo(); thread.start(); //thread.sleep(100); thread.interrupt(); //中断线程 } @Override public void run() { System.out.println("in run()"); while(!Thread.currentThread().isInterrupted()) { System.out.println("thread running"); try { Thread.sleep(100); }catch(InterruptedException e) { System.out.println("InterruptedException"); //break; } } System.out.println("thread interrupted"); } }
public class ThreadDemo extends Thread{ public static void main(String[] args) throws InterruptedException { Thread thread = new ThreadDemo(); thread.start(); thread.sleep(100); thread.interrupt(); //中断线程 } @Override public void run() { while(!Thread.currentThread().isInterrupted()) { System.out.println("thread running"); try { Thread.sleep(100); }catch(InterruptedException e) { System.out.println("InterruptedException"); Thread.currentThread().interrupt(); //再次中断线程 //break; } } System.out.println("thread interrupted"); } }
public class ThreadDemo extends Thread{ public static void main(String[] args){ try{ ThreadDemo thread = new ThreadDemo(); thread.start(); thread.sleep(100); thread.interrupt(); //中断线程 thread.sleep(100); thread.printStr(); thread.interrupt(); //第三次中断线程 thread.printStr(); thread.join(); }catch(InterruptedException e){ e.printStackTrace(); } } private void printStr(){ System.out.println("thread obj"); } @Override public void run() { while(!Thread.currentThread().isInterrupted()) { System.out.println("thread running"); try { Thread.sleep(100); }catch(InterruptedException e) { System.out.println("InterruptedException"); Thread.currentThread().interrupt(); //再次中断线程 } } System.out.println("thread interrupted"); } }运行结果: