无法从排定的执行程序服务中设置布尔值标志

时间:2019-09-06 07:04:10

标签: java boolean scheduledexecutorservice

我需要定期运行一段代码。此代码检查条件并设置布尔标志。 为了定期运行它,我使用了Option Explicit Public Sub PrintSplitData() Dim ws As Worksheet Set ws = ThisWorkbook.ActiveSheet 'better to name the sheet unless you do it on many different sheets 'Set ws = ThisWorkbook.Worksheets("YouDataSheetName") Dim LastRow As Long LastRow = ws.Cells(ws.Rows.Count, 1).End(xlUp).Row Dim iStartRow As Long, iEndRow As Long For iStartRow = 1 To LastRow Step 25 iEndRow = iStartRow + 24 iEndRow = IIf(iEndRow > LastRow, LastRow, iEndRow) 'limit it to LastRow Debug.Print iStartRow, iEndRow 'this is just for illustrating ws.Range("A1").AutoFilter Field:=1, Criteria1:=">=" & iStartRow, Operator:=xlAnd, Criteria2:="<=" & iEndRow ws.AutoFilterMode = False Next iStartRow End Sub 方法。

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

请告知。

scheduledExecutorService.scheduleAtFixedRate

可运行代码:

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();
    }

}

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

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());

    }

}

第二客户端类:

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;
    }
}

1 个答案:

答案 0 :(得分:1)

这是两个不同的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进程中检查全局标志。