Java代码:
package Threads; /** * Created by Frank */ public class StopBoolean extends Thread { // 确保变化对其它线程可见(主要是主线程要可见) protected volatile boolean done = false; public void run() { while (!done) { System.out.println("StopBoolean running"); try { sleep(720); } catch (InterruptedException e) { return; } } System.out.println("StopBoolean finished"); } public void shutDown() { done = true; } public static void main(String[] args) throws InterruptedException { StopBoolean t1 = new StopBoolean(); t1.start(); Thread.sleep(1000 * 5); t1.shutDown(); } }
原创文章,作者:奋斗,如若转载,请注明出处:https://blog.ytso.com/15857.html