Java – number of active tasks using ThreadPoolExecutor

I am using ThreadPoolExecutor to perform tasks in my java application I have a request. I want to get the number of active tasks in the queue at any point in time in the execution program queue I looked at the Javadoc ThreadPoolExecutor and found two related methods: gettaskcount () and getcompletedtaskcount ()

According to the document, I can get the number of planned tasks and completed tasks from the above two methods respectively But I can't find a solution to get the number of active tasks in the queue at any point in time I can do this:

getTaskCount() = getCompletedTaskCount() + Failed tasks + active tasks

However, the number of failed tasks cannot be obtained directly to achieve the expected calculation

Did I miss anything here?

Solution

I don't think you need to know the count of failures by the calculations you're trying to use

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

It will be (about) enough

Alternatively, you can use getqueue () and size ():

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

This answer assumes that you are looking for an "unfinished" count Your question contradicts yourself, so I'm not quite sure what you're asking If this is not correct, please reply to my comments on your question and I will update this answer accordingly

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>