![](/img/trans.png)
[英]Is it possible to execute scheduled task only one time and not in all thread pool?
[英]ScheduledThreadPoolExecutor with one scheduled task(Runnable) keeps reporting back job counter + 1 all the time
當我打印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()在文檔中指出:
返回已計划執行的任務總數 。 由於任務和線程的狀態在計算過程中可能會動態變化,因此返回的值只是一個近似值, 但是在后續調用中不會降低
因此,getTaskCount()返回池已執行的任務總數。
我不確定您的目標是什么,但是如果您想要當前正在執行的任務數,請嘗試使用getActiveCount()。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.