标签:并发
public class StateStopTask implements Runnable{ private static volatile boolean isCancled = false; public void run() { while(true) { if(isCancled == true) { System.out.println("StateStopTask is stop"); return; } } } public static void main(String []args) { new Thread(new StateStopThread()).start(); Thread.sleep(3000); isCancled = true; } }
public class StateStopTask implements Runnable{ public void run() { while(!Thread.currentThread().isInterrupted()) { System.out.println("StateStopTask is running"); } System.out.println("StateStopTask is stopping"); } public static void main(String []args) { Thread t = new Thread(new StateStopThread()).start(); Thread.sleep(3000); t.interrupt(); } }
public class InterruptedTask implements Runnable{ public void run() { while(!Thread.currentThread().interrupted()) { try { System.out.println("InterruptedTask running"); Thread.sleep(100); } catch (InterruptedException e) { System.out.println("interrupted by InterruptedException ") } } System.out.println("InterruptedTask Interrupted") } }
标签:并发
原文地址:http://blog.csdn.net/troy__/article/details/40627701