【问题标题】:How to change value of period for scheduleAtFixedRate when program is running程序运行时如何更改 scheduleAtFixedRate 的周期值
【发布时间】:2017-12-16 22:47:04
【问题描述】:

我正在编写计数器的分布式网络,我使用 ScheduledExecutorService 类中的 scheduleAtFixedRate 在网络中的计算机之间发送数据包,并在经过 synPeriod 时在每台计算机上打印计数器的当前值。 当我键入命令“set period [value]”时,应该在 [value] 上更改 scheduleAtFixedRate 的周期。我更新了 period 变量的值,但它不影响 scheduleAtFixedRate 中的 period.. 是否有可能在它工作时更改该值?或者有什么办法是停止执行器,然后用另一个周期再次调用它?

Runnable printer = () -> {
        try
        {
            //send packet with value of counter
            Thread.sleep(200);
        }
        catch (IOException e)
        {
        }
        catch (InterruptedException e)
        {
        }
        System.out.println("Counter value: " + counter);
    };
ScheduledExecutorService printerExecutor = Executors.newScheduledThreadPool(1);
    printerExecutor.scheduleAtFixedRate(printer, synPeriod, synPeriod, TimeUnit.SECONDS);

编辑: 我已经读到 ScheduledFuture 会很有用并且有效!。

【问题讨论】:

    标签: java counter scheduledexecutorservice


    【解决方案1】:

    您必须取消任务并使用所需值重新安排它

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-05-28
    • 2013-12-14
    • 2011-07-09
    • 1970-01-01
    • 1970-01-01
    • 2011-04-13
    • 1970-01-01
    相关资源
    最近更新 更多