如果未启动,如何仅执行调度程序?

发布于 2025-02-08 13:27:28 字数 255 浏览 1 评论 0原文

ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
if(executor.isShutdown()) {
    executor.scheduleAtFixedRate(helloRunnable, 0, 1, TimeUnit.SECONDS);
}

每次发生事件时都执行此代码,我该如何仅第一次执行它

? >但是它不起作用

ScheduledExecutorService executor = Executors.newScheduledThreadPool(1);
if(executor.isShutdown()) {
    executor.scheduleAtFixedRate(helloRunnable, 0, 1, TimeUnit.SECONDS);
}

This piece of code is executed everytime a event happens, how do i make it only execute for the first time?(like, if the scheduler is starter, do not execute)

I tried using executor.isShutdown() but it does not work

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

紫瑟鸿黎 2025-02-15 13:27:28
  1. 您可以使用atomicboolean来管理对执行者的访问:
    private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
    private static final AtomicBoolean IS_ACTIVE = new AtomicBoolean(false);

    private void startScheduler() {
        if (IS_ACTIVE.compareAndSet(false, true)) {
            executor.scheduleAtFixedRate(() -> {}, 0L, 5L, TimeUnit.SECONDS);
        }
    }
  1. 或者您可以使用同步和懒惰的初始化来使其起作用:
    private volatile ScheduledExecutorService scheduler;

    public void startScheduler() {
        if (scheduler == null) {
            this.initAndStart();
        }
    }

    private synchronized void initAndStart() {
        if (scheduler == null) {
            scheduler = Executors.newSingleThreadScheduledExecutor();
            scheduler.schedule(() -> log.info("Started, Thread: {}", Thread.currentThread().getName()), 0L, TimeUnit.SECONDS);
        }
    }

note note :它将尝试尝试要阻止设法调用initandstart的当前类和线程也将导致阻塞,但是一旦调度程序初始化

  1. You can use AtomicBoolean to manage access to the executor:
    private final ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
    private static final AtomicBoolean IS_ACTIVE = new AtomicBoolean(false);

    private void startScheduler() {
        if (IS_ACTIVE.compareAndSet(false, true)) {
            executor.scheduleAtFixedRate(() -> {}, 0L, 5L, TimeUnit.SECONDS);
        }
    }
  1. Or you can use synchronized and lazy initialization to make it work:
    private volatile ScheduledExecutorService scheduler;

    public void startScheduler() {
        if (scheduler == null) {
            this.initAndStart();
        }
    }

    private synchronized void initAndStart() {
        if (scheduler == null) {
            scheduler = Executors.newSingleThreadScheduledExecutor();
            scheduler.schedule(() -> log.info("Started, Thread: {}", Thread.currentThread().getName()), 0L, TimeUnit.SECONDS);
        }
    }

Note: It will try to block current class and threads which managed to call initAndStart will also cause blocking, but as soon as scheduler is initialized there will be no blocking

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文