zl程序教程

您现在的位置是:首页 >  后端

当前栏目

Java通过在主循环中判断Boolean来停止线程详解编程语言

JAVA循环线程编程语言 详解 通过 判断 停止
2023-06-13 09:20:45 时间
// 确保变化对其它线程可见(主要是主线程要可见) 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(); }

 

15857.html

cjava