gpt4 book ai didi

java - TimerTask 停止所有其他执行

转载 作者:行者123 更新时间:2023-12-01 09:37:03 27 4
gpt4 key购买 nike

我有一个程序,它启动一个计时器,然后在停止之前每秒重复一次代码。发生的情况是它不会停止来自 main 的执行。我假设它只是一个与之一起运行的线程,但通过查看帖子,我仍然不确定如何暂时停止它。

final class DogDoor{
private static DogDoor dogDoor;
private boolean open;

private DogDoor(){
open=false;
}

public static synchronized DogDoor getDogDoor(){
if(dogDoor==null){
dogDoor=new DogDoor();
}
return dogDoor;
}

public void activate(){
open=!open;
System.out.println(open? "Door is open!": "Door is closed!");
if(open){
autoClose();
}
}
public void autoClose(){
System.out.print("Door closing in 5 seconds . . .");

//right before it starts the timer and task
timer();

//then resume here after the timer completes
activate();
}
public void timer(){
new java.util.Timer().scheduleAtFixedRate(
new java.util.TimerTask(){

//Prefer to stop until this completes
int s=5;
@Override
public void run(){
System.out.print(" .");
if((s--)==0){
cancel();
}
}
}, 0, 1000);
}
}

final class Remote{
private static Remote remote;
private boolean windowsLocked;
private int lockCode;

public Remote(){
windowsLocked=true;
generateLockCode();
}

public static synchronized Remote getRemote(){
if(remote==null){
remote=new Remote();
}
return remote;
}
public String getLockCode(){
return String.format("%03d", lockCode);
}

public boolean windowsLocked(){
return windowsLocked;
}
public void generateLockCode(){
lockCode=(int)(Math.random()*1000);
}
public void toggleDoor(){
DogDoor.getDogDoor().activate();
}
public void fixWindows(){
if(passCodeVerifier()){
windowsLocked=!windowsLocked;
System.out.println(windowsLocked? "Windows locked!": "Windows unlocked!");
}
else{
System.out.println("Invalid passcode!");
}
}
public boolean passCodeVerifier(){
Scanner scanner=new Scanner(System.in);
System.out.print("Enter three digit keypad code: ");
return scanner.hasNext("^\\d{3}$") && Integer.parseInt(scanner.next())==(lockCode);
}
}

public class DogDoorDemo{
public static void main(String[] args){
System.out.println("Dog door demo.");
System.out.println();

System.out.println(Remote.getRemote().getLockCode());
Remote.getRemote().toggleDoor();

//and finally resume here
Remote.getRemote().fixWindows();
}
}

最佳答案

正如文档中提到的,计时器是:

A facility for threads to schedule tasks for future execution in a **background thread**

这意味着使用 TimerTask 运行的程序的任何部分都将在与主线程不同的线程上运行,因此不会阻止主线程上的执行。

另一种解决方案是将线程 hibernate 所需的秒数,如下所示(用此方法替换您的 timer() 方法并捕获 InterruptedException。):

void timer(int seconds)throws InterruptedException{
for(int i = 0; i < seconds; i++){
Thread.sleep(1000);
//Output whatever message you want to appear every second. The variable 'i' will hold the current second in the countdown.
}
}

并将您的 autoClose 方法替换为:

public void autoClose(){
System.out.print("Door closing in 5 seconds . . .");

//right before it starts the timer and task
timer(5);

//then resume here after the timer completes
activate();
}

Thread#sleep 抛出 InterruptedException 因此必须适当处理。

关于java - TimerTask 停止所有其他执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38814533/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com