62,614
社区成员
发帖
与我相关
我的任务
分享
public class Main extends Thread {
volatile boolean flag = false;
@Override
public void run() {
while (!flag) {
System.out.println("the thread is run ...");
try {
TimeUnit.SECONDS.sleep(1L);
} catch (InterruptedException e) {
// ignore
}
}
}
public void goOn() {
if (flag) {
flag = false;
run();
}
}
public static void main(String[] args) throws InterruptedException {
Main td = new Main();
System.out.println("``````````````````````````````````````");
td.start();
TimeUnit.SECONDS.sleep(2L);
td.flag = true;
System.out.println("use interrupt()...");
TimeUnit.SECONDS.sleep(4L);
System.out.println("use goOn()...");
td.goOn();
}
}