为什么 ScheduledExecutorService 没有按预期工作?
我正在创建一个 10 个线程的池 每个线程运行3秒 我将每个任务的启动周期设置为0.5秒
问题是,如果池由10个线程组成,启动周期为0.5秒,为什么从第一个线程启动第二个线程需要3秒? 毕竟,10 个线程应该在 0.5 秒内同时启动,依此类推。
如果我使用 newSingleThreadScheduledExecutor 这是可以理解的,但我使用的是带有 10 个线程的 newScheduledThreadPool。
public class Main17 {
public static void main(String[] args) {
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);
Runnable r = ()-> {
System.out.println("hello "+System.currentTimeMillis());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
};
scheduledExecutorService.scheduleAtFixedRate(r,0, 500, TimeUnit.MILLISECONDS);
}
}
结果
hello 1646991199804
hello 1646991202816 // Interval greater than 0.5 seconds
hello 1646991205831
hello 1646991208840
hello 1646991211850
I am creating a pool of 10 threads
Each thread runs for 3 seconds
I set the startup period of each task to 0.5 seconds
The question is, if the pool consists of 10 threads, and the startup period is 0.5 seconds, why does it take 3 seconds from the first thread to start the second one?
After all, 10 threads should start in 0.5 seconds at once, and so on.
If I were to use newSingleThreadScheduledExecutor it would be understandable, but I'm using newScheduledThreadPool with 10 threads.
public class Main17 {
public static void main(String[] args) {
ScheduledExecutorService scheduledExecutorService = Executors.newScheduledThreadPool(10);
Runnable r = ()-> {
System.out.println("hello "+System.currentTimeMillis());
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
};
scheduledExecutorService.scheduleAtFixedRate(r,0, 500, TimeUnit.MILLISECONDS);
}
}
result
hello 1646991199804
hello 1646991202816 // Interval greater than 0.5 seconds
hello 1646991205831
hello 1646991208840
hello 1646991211850
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
线程在这里不起作用。您必须了解您正在使用的方法的效果。
scheduleAtFixedRate(Runnable command,长初始延迟,长周期,TimeUnit单位)
:重要部分:
执行器等待 500ms 的间隔或可运行对象的执行时间,以较长者为准。在您的情况下,这是运行可运行程序的时间,因此时间戳的差异恰好是 3 秒。
编辑:您可以查看这篇文章,其中有人遇到了同样的问题。有两个答案可以达到您想要的结果。
The threads have no effect here. You have to understand the effect of th method you are using.
Documentation for
scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)
:Important part:
The executor waits either the interval of 500ms or the execution time of the runnable, whichever is longer. In your case, it is the time to run the runnable and therefore the difference in your timestamps is exactly 3 seconds.
EDIT: You can look at this post, where someone had the same problem. There are two answers that achieve your desired result.
引用
ScheduledExecutorService.scheduleAtFixedRate()
官方文档:来源:ScheduledExecutorService 文档
Quoting the Oficial documentation of
ScheduledExecutorService.scheduleAtFixedRate()
:Source: ScheduledExecutorService documentation
请参阅 ScheduleAtFixedRate jdk 文档
您的任务至少需要 3 秒才能完成。
see the scheduleAtFixedRate jdk doc
Your task need at least 3 second to complete.