gpt4 book ai didi

java - 具有一个计划任务(可运行)的 ScheduledThreadPoolExecutor 始终报告作业计数器 + 1

转载 作者:行者123 更新时间:2023-11-29 06:19:22 25 4
gpt4 key购买 nike

当我打印 s.getTaskCount() 时,我得到一个不断增加的(如 1、2、3、4、5、6、7...)任务计数。我不明白为什么。

public class MyTask implements Runnable
{
public void run()
{
System.out.println("whatever....");
}
}


ScheduledThreadPoolExecutor s = new ScheduledThreadPoolExecutor(3);
s.scheduleAtFixedRate(new MyTask(), 0, 10, TimeUnit.SECONDS);


while(1>0)
{
try
{
System.out.println("TASK COUNT: "+s.getTaskCount());
Thread.sleep(60000);
}
catch(InterruptedException e)
{
System.out.println(e);
}
}

最佳答案

ScheduledThreadPoolExecutor.getTaskCount() 在文档中指出:

Returns the approximate total number of tasks that have been scheduled for execution. Because the states of tasks and threads may change dynamically during computation, the returned value is only an approximation, but one that does not ever decrease across successive calls

因此,getTaskCount() 返回池中已执行的任务总数。

我不确定您的目标是什么,但如果您想要当前正在执行的任务数,请尝试 getActiveCount()。

关于java - 具有一个计划任务(可运行)的 ScheduledThreadPoolExecutor 始终报告作业计数器 + 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3753336/

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