gpt4 book ai didi

java - 使用循环重复线程与使用 scheduleAtFixedRate() 重复运行

转载 作者:行者123 更新时间:2023-11-29 05:31:32 26 4
gpt4 key购买 nike

final Runnable refresh = new Refresh(params...);
service = Executors.newScheduledThreadPool(1);
service.scheduleAtFixedRate(refresh, 0, 2000, TimeUnit.MILLISECONDS);

// OR

final Thread refresh = new Refresh(params...);
refresh.start(); // In the run() method there is a loop with a sleep of 2000 ms

以上哪种重复一段代码的方法是首选,为什么?

最佳答案

它在功能上是等价的,但前者更灵活且更好地分离职责(SRP):任务不应该对它的运行方式或时间负责......

关于java - 使用循环重复线程与使用 scheduleAtFixedRate() 重复运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20928767/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com