gpt4 book ai didi

java - 在特定时间仅在特定持续时间内运行一次 java 线程,然后停止

转载 作者:行者123 更新时间:2023-11-30 06:15:58 24 4
gpt4 key购买 nike

我正在尝试创建一个线程,该线程将在特定时间、特定持续时间内执行,并且仅运行一次,然后停止,直到再次调用它。

问题在于,每当线程执行时,它并没有执行到持续时间,只是打印一个“Hi”,然后立即停止。如果我删除该标志,线程将永远运行。

  public void startExecutionAt(int targetHour, int targetMin, int duration)
{
log.info("Create thread!");
Runnable taskWrapper= new Runnable(){
@Override
public void run()
{
if(runsFirst==true){
log.info("Thread inside!");
WateringScheduler.execute();
startExecutionAt(targetHour, targetMin, duration);
}
else{
log.info("Thread stoped!");
stop();
//close the led
}
runsFirst=false;
}

};

long delay = computeNextDelay(targetHour, targetMin);
System.out.println("Delay is : " + delay + " Duration is: " +duration);
executorService.scheduleAtFixedRate(taskWrapper, delay,duration, TimeUnit.SECONDS);
}


private static long computeNextDelay(int targetHour, int targetMin) {
LocalDateTime localNow = LocalDateTime.now();
ZoneId currentZone = ZoneId.systemDefault();
ZonedDateTime zonedNow = ZonedDateTime.of(localNow, currentZone);
ZonedDateTime zonedNextTarget = zonedNow.withHour(targetHour).withMinute(targetMin);

if (zonedNow.compareTo(zonedNextTarget) > 0) {
zonedNextTarget = zonedNextTarget.plusDays(1);
}

Duration duration = Duration.between(zonedNow, zonedNextTarget);
return duration.getSeconds();
}


public void stop()
{
executorService.shutdown();
try {
executorService.awaitTermination(1, TimeUnit.DAYS);
} catch (InterruptedException ex) {
log.error("Error");;
}
}
public static void execute(){
System.out.println("Hi");
//open the led;
}

最佳答案

我对 ScheduledExecutorService 的使用感到困惑。

根据 javadocs,“ScheduledFuture 计划创建并执行一次性操作,该操作在给定延迟后启用。”

下面的代码执行线程一次,并在 (delay+duration)+1 后停止线程。

 public void startExecutionAt(int targetHour, int targetMin, int duration)
{
log.info("Create thread!");
long delay = computeNextDelay(targetHour, targetMin);
final Runnable executeR = new Runnable() {
public void run() {
try {
execute();
}catch(Exception ex) {
log.error("Error in thread" + ex.getMessage());
}
}
};

final ScheduledFuture<?> executeHandler = executorService.scheduleAtFixedRate(executeR,delay, duration, TimeUnit.SECONDS);

executorService.schedule(
new Runnable() {
public void run() {
log.info("Thread is stopping");
executeHandler.cancel(true);
}
}, delay+duration+1, TimeUnit.SECONDS);

System.out.println("Delay is : " + delay + " Duration is: " +duration);
log.info("Thread out");
}


private static long computeNextDelay(int targetHour, int targetMin) {
LocalDateTime localNow = LocalDateTime.now();
ZoneId currentZone = ZoneId.systemDefault();
ZonedDateTime zonedNow = ZonedDateTime.of(localNow, currentZone);
ZonedDateTime zonedNextTarget = zonedNow.withHour(targetHour).withMinute(targetMin);

if (zonedNow.compareTo(zonedNextTarget) > 0) {
zonedNextTarget = zonedNextTarget.plusDays(1);
}

Duration duration = Duration.between(zonedNow, zonedNextTarget);
return duration.getSeconds();
}


public void stop()
{
executorService.shutdown();
try {
executorService.awaitTermination(1, TimeUnit.DAYS);
} catch (InterruptedException ex) {
log.error("Error");;
}
}

关于java - 在特定时间仅在特定持续时间内运行一次 java 线程,然后停止,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49183293/

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