Commit 9ffa454a57f457b51fd257cf569a445021c95ba3
1 parent
c6021f331c
Exists in
master
测试文件提交
Showing 1 changed file with 28 additions and 0 deletions
center.manager/src/test/java/center/manager/test/user/ThreadFlag.java
View file @
9ffa454
| 1 | +package center.manager.test.user; | |
| 2 | + | |
| 3 | +public class ThreadFlag extends Thread { | |
| 4 | + public volatile boolean exit = false; | |
| 5 | + | |
| 6 | + public void run() { | |
| 7 | + while (!exit) { | |
| 8 | + // System.out.println("i`m ok!"); | |
| 9 | + } | |
| 10 | + } | |
| 11 | + | |
| 12 | + public static void main(String[] args) throws Exception { | |
| 13 | + ThreadFlag thread = new ThreadFlag(); | |
| 14 | + thread.start(); | |
| 15 | + int i = 0; | |
| 16 | + while (true) { | |
| 17 | + if (i >= 50) { | |
| 18 | + thread.exit = true; // 终止线程thread | |
| 19 | + thread.join(); | |
| 20 | + System.out.println("线程退出!"); | |
| 21 | + } | |
| 22 | + System.out.println(thread.isAlive() + " " + i); | |
| 23 | + sleep(500); // 主线程延迟5秒 | |
| 24 | + i++; | |
| 25 | + } | |
| 26 | + | |
| 27 | + } | |
| 28 | +} |