繁体   English   中英

停止预定的 ScheduledExecutorService

[英]Stop a scheduled ScheduledExecutorService

ScheduledExecutorService 在从下面类的关闭方法中调用其关闭方法后仍在运行。

我没想到关机后会看到 perioudRun 的调用 运行方法shutdown 运行方法periodicRun-

我应该怎么做才能确保计划运行被取消?

class test {

    private final ScheduledExecutorService scheduler =
            Executors.newScheduledThreadPool(1);

test() {
        

        scheduler.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                funcA("periodicRun-");
            }
        }, 15, 15, TimeUnit.SECONDS);
    }

private void funcA(String path) {
LOGGER.info("Running the method " + path)
}
public void shutdown()  {
        long startTimeMs = System.currentTimeMillis();
        scheduler.shutdown(); // Disable new tasks from being submitted

        try {
            // Wait a while for existing tasks to terminate
            if (!scheduler.awaitTermination(initialTerminationSeconds, TimeUnit.SECONDS)) {
                scheduler.shutdownNow(); // Cancel currently executing tasks
                // Wait a while for tasks to respond to being cancelled
                scheduler.awaitTermination(retryTerminationSeconds, 
            }
        } catch (InterruptedException ie) {
            // (Re-)Cancel if current thread also interrupted
            scheduler.shutdownNow();
            // Preserve interrupt status
            Thread.currentThread().interrupt();
        }
        
    }
        funcA("shutdown-");
}

}```

您提供的代码无法编译。 我在没有编译的地方做了一些更正和一些假设,以下代码按预期工作,即在调用shutdown()后不打印“periodicRun”:

class Test {
    
    public static void main(String[] args) {
        Test test = new Test();
        test.test(); // CALL TEST METHOD
        
        Thread t = new Thread(() -> {
            try {
                Thread.sleep(2500);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            test.shutdown(); // CALL SHUTDOWN METHOD
        });
        t.start();
    }

    private final int initialTerminationSeconds = 1;
    private final int retryTerminationSeconds = 1;
    
    private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1);

    void test() {
        scheduler.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                funcA("periodicRun-");
            }
        }, 15, 15, TimeUnit.SECONDS);
    }

    private void funcA(String path) {
        System.out.println("Running the method " + path);
    }
    
    public void shutdown()  {
        long startTimeMs = System.currentTimeMillis();
        scheduler.shutdown(); // Disable new tasks from being submitted

        try {
            // Wait a while for existing tasks to terminate
            if (!scheduler.awaitTermination(initialTerminationSeconds, TimeUnit.SECONDS)) {
                scheduler.shutdownNow(); // Cancel currently executing tasks
                // Wait a while for tasks to respond to being cancelled
                scheduler.awaitTermination(retryTerminationSeconds, TimeUnit.SECONDS);
            }
        } catch (InterruptedException ie) {
            // (Re-)Cancel if current thread also interrupted
            scheduler.shutdownNow();
            // Preserve interrupt status
            Thread.currentThread().interrupt();
        }
        funcA("shutdown-");
    }
}

由于此代码按预期工作,您的代码中的问题要么在其他地方,要么我对您的代码所做的假设和更正不正确。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM