gpt4 book ai didi

java - 无法从计划的执行程序服务设置 boolean 标志

转载 作者:行者123 更新时间:2023-11-30 05:29:41 24 4
gpt4 key购买 nike

我需要定期运行一段代码。此代码检查条件并设置 boolean 标志。为了定期运行它,我使用 scheduledExecutorService.scheduleAtFixedRate 方法。

调度程序工作正常,但无法设置标志。标志值始终为 false。

请指教。

package healthCheck;

import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

public class Test {
private static ScheduledExecutorService scheduler;

public static void main(String[] args)
throws InterruptedException, ExecutionException {
scheduler = Executors.newSingleThreadScheduledExecutor();
Future<?> scheduledFuture = scheduler.scheduleAtFixedRate(
new SomeFiveSecondelyJob(), 1, 5, TimeUnit.SECONDS);
System.out.println("Flag at scheduler"+scheduledFuture.get());
}

public static void contextDestroyed() {
scheduler.shutdownNow();
}

}

可运行代码:

package healthCheck;

import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

public class SomeFiveSecondelyJob implements Runnable {

@Override
public void run() {
URL url;
try {
url = new URL("https://www.google.co.in/");
HttpsURLConnection connection =
(HttpsURLConnection) url.openConnection();
connection.setRequestMethod("GET");
connection.setConnectTimeout(5000);

int code = connection.getResponseCode();

if (code / 100 == 2) {
ItSystemFlag.setFlag(true);
System.out.println("System is up");

} else {
ItSystemFlag.setFlag(false);
System.out.println("System unavailable: " + code);

}

} catch (Exception e) {
ItSystemFlag.setFlag(false);
e.printStackTrace();
}
System.out.println(ItSystemFlag.getFlag());

}

}

我使用静态 volatile 变量作为标志:

package healthCheck;

public class ItSystemFlag {
private static volatile Boolean flag = false;

public static void setFlag(Boolean flag) {
ItSystemFlag.flag = flag;
System.out.println("seting the flag to :" + ItSystemFlag.flag);
}
public static Boolean getFlag() {
return ItSystemFlag.flag;
}
}

第二个客户端类:

package healthCheck;

public class Test2 {

public static void main(String[] args) throws InterruptedException {
for(int i=0;i<20;i++) {
System.out.println("From client "+ItSystemFlag.getFlag());
Thread.sleep(6000);
}
}

}

最佳答案

这是两个不同的java进程。全局变量不在它们之间共享。尝试:

public class Test {
private static ScheduledExecutorService scheduler;

public static void main(String[] args)
throws InterruptedException, ExecutionException {
scheduler = Executors.newSingleThreadScheduledExecutor();
Future<?> scheduledFuture = scheduler.scheduleAtFixedRate(
new SomeFiveSecondelyJob(), 1, 5, TimeUnit.SECONDS);
System.out.println("Flag at scheduler"+scheduledFuture.get());

for(int i=0;i<20;i++) {
System.out.println("From client "+ItSystemFlag.getFlag());
Thread.sleep(6000);
}
}

public static void contextDestroyed() {
scheduler.shutdownNow();
}


}

首先运行调度程序,然后在同一个 java 进程中检查全局标志。

关于java - 无法从计划的执行程序服务设置 boolean 标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57817234/

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