Number of active tasks using ThreadPoolExecutor

后端 未结 2 1913
野趣味
野趣味 2021-01-05 10:47

I am using a ThreadPoolExecutor to execute tasks in my Java application. I have a requirement where I want to get the number of active tasks in the queue at any point in tim

2条回答
  •  日久生厌
    2021-01-05 11:42

    I don't think you need to know the failed count with the calculation you're trying to use.

    long submitted = executor.getTaskCount();
    long completed = executor.getCompletedTaskCount();
    long notCompleted = submitted - completed; // approximate
    

    Would be (approximately) sufficient.


    Alternatively, you can use getQueue() with size():

    int queued = executor.getQueue().size();
    int active = executor.getActiveCount();
    int notCompleted = queued + active; // approximate
    

    This answer presumes you're looking for a "not yet completed" count. Your question contradicts itself so I'm not completely certain what you're asking. Reply to my comment on your question if this is incorrect, and I'll update this answer accordingly.

提交回复
热议问题