如果未启动,如何仅执行调度程序?
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
atomicboolean
来管理对执行者的访问:同步
和懒惰的初始化来使其起作用:note note :它将尝试尝试要阻止设法调用
initandstart
的当前类和线程也将导致阻塞,但是一旦调度程序初始化AtomicBoolean
to manage access to the executor:synchronized
and lazy initialization to make it work: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