Java通过在主循环中判断Boolean来停止线程的方法实例

 
Java通过在主循环中判断Boolean来停止线程的方法实例
2017-04-14 07:58:47 /故事大全

本文实例讲述了Java通过在主循环中判断Boolean来停止线程的方法。分享给大家供大家参考,具体如下:

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();

}

}

所属专题:
如果您觉得本文或图片不错,请把它分享给您的朋友吧!

 
故事大全
 
版权所有- © 2012-2025 · 故事大全 SITEMAP站点地图-Foton Auman手机看故事 站点地图-Foton Auman