Java 无法从计划执行器服务设置布尔标志
我需要定期运行一段代码。此代码检查一个条件并设置一个布尔标志。 为了定期运行它,我使用了Java 无法从计划执行器服务设置布尔标志,java,boolean,scheduledexecutorservice,Java,Boolean,Scheduledexecutorservice,我需要定期运行一段代码。此代码检查一个条件并设置一个布尔标志。 为了定期运行它,我使用了scheduledExecutorService.scheduleAtFixedRate方法 调度程序工作正常,但无法设置标志。 标志值始终为false 请告知 package healthCheck; import java.util.concurrent.ExecutionException; import java.util.concurrent.Executors; import java.util
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;
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);
}
}
}
包装健康检查;
公共类Test2{
公共静态void main(字符串[]args)引发InterruptedException{
对于(int i=0;i这是两个不同的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();
}
}
公共类测试{
专用静态调度器ExecutorService调度器;
公共静态void main(字符串[]args)
抛出InterruptedException、ExecutionException{
scheduler=Executors.newSingleThreadScheduledExecutor();
Future scheduledFuture=scheduler.scheduleAtFixedRate(
新的SomeFiveSecondelyJob(),1,5,TimeUnit.SECONDS);
System.out.println(“调度程序处的标志”+scheduledFuture.get());
对于(int i=0;在一个JVM的内存中设置的iA标志在另一个JVM的内存中不可能可见。如果要在两个不同的进程(JVM)之间通信,则必须使用文件,例如套接字。而不是内存标志。谢谢,在同一个JVM进程中使用时,它可以正常工作。
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();
}
}