gpt4 book ai didi

java - 线程已停止但仍在运行(Java)

转载 作者:行者123 更新时间:2023-12-01 12:33:15 26 4
gpt4 key购买 nike

这是我的代码。

public class SetTimer extends Thread{
// set it on false from outside you want to stop
private boolean runSignal = true;

// sleep time
private final long SLEEP_TIME = 10*1000; //900 detik = 15 menit

PageReaderKontan pn = new PageReaderKontan();
PageReaderBisnis pb = new PageReaderBisnis();
FileListener fileListener = new FileListener();

Timer timer = new Timer();

boolean keep = true;

public void run(){
while (runSignal){
pn.run();
System.out.println("Kontan Finished.\n");
pb.run();
System.out.println("Bisnis Finished.\n");
fileListener.run();
doSleep(); //go to sleep
checkTime();


}
}

public void checkTime(){
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Date alarmTime = calendar.getTime();
timer.schedule(new TimerTask() {
@Override
public void run() {
stopRunning();
System.out.println("Compiling runs here...");
timer.cancel();
timer.purge();
return;
}
}, alarmTime);
System.out.println("Time checked.");
}
//sleep between listening folder iteration
public void doSleep(){
try{
System.out.println("I go to sleep for 10 seconds now...");
sleep(SLEEP_TIME);
}
catch (InterruptedException e){
System.out.println(e.getMessage());
}
}

//stop running this thread when application stops
public void stopRunning(){
System.out.println("Bye!");
this.runSignal = false;
}
}

应用程序打印“Compiling running here...”后,它会返回并一遍又一遍地运行线程。我的代码怎么错了??请帮助我,我真的很难理解线程是如何工作的。多谢。 :)

编辑 1 - 这是控制台输出。

Kontan Finished.

Bisnis Finished.

I go to sleep for 10 seconds now...

Time checked.

Bye!

Compiling runs here...

Kontan Finished.

Bisnis Finished.

I go to sleep for 10 seconds now...

Time checked.

Bye!

Compiling runs here...

...........

编辑 2 - 更新...我正在使用 System.exit() 跳出无限循环并结束应用程序。

public void checkTime(){
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, 9);
calendar.set(Calendar.MINUTE, 0);
calendar.set(Calendar.SECOND, 0);
Date alarmTime = calendar.getTime();
timer.schedule(new TimerTask() {
@Override
public void run() {
stopRunning();
System.out.println("Compiling runs here...");
System.out.println("Application is closing...");
System.exit(0);
}
}, alarmTime);
System.out.println("Time checked.");
}

最佳答案

请将您的 runSignal boolean 值声明为 volatile 。 volatile 保证读取变量的线程在读取变量之前看到另一个线程对其所做的更改。在您的情况下,看起来正在更改 runSignal 值的 Timer 线程没有被正在执行 run 的其他线程读取,因此它不会停止。

private volatile boolean runSignal = true;

编辑:-通过执行以下更改,您的代码绝对可以正常工作。进行此更改的原因是为了避免尝试安排已取消的计时器时出现任何异常。

编写一个名为:-

的方法
public void stopTimer() {
timer.cancel();
timer.purge();
}

从您的运行方法中调用此方法,如下所示:-

public void run(){
while (runSignal){
pn.run();
System.out.println("Kontan Finished.\n");
pb.run();
System.out.println("Bisnis Finished.\n");
fileListener.run();
doSleep(); //go to sleep
checkTime();
}
stopTimer();
}

并从计时器计划方法中删除计时器取消,如下所示:-

timer.schedule(new TimerTask() {
@Override
public void run() {
stopRunning();
System.out.println("Compiling runs here...");
return;
}
}, alarmTime);

关于java - 线程已停止但仍在运行(Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25778159/

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