Java并发

This commit is contained in:
罗祥
2019-11-27 16:54:51 +08:00
parent fdb2e9404c
commit 7a257140b7
8 changed files with 472 additions and 22 deletions

View File

@ -0,0 +1,26 @@
package com.heibaiying.stop;
/**
* 线程终止
*/
public class ThreadStop {
private static volatile boolean stopFlag = true;
public static void main(String[] args) throws InterruptedException {
Thread thread = new Thread(() -> {
while (stopFlag) {
try {
Thread.sleep(100);
System.out.println("持续输出");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
thread.start();
Thread.sleep(3 * 1000);
stopFlag = false;
System.out.println("线程终止");
}
}