Reputation: 4028
I have a server that has multiple worker threads implemented using java executorService (ie. a thread pool)
My problem is that I am not able to log at every second, the length of jobs waiting to be processed if there is no idle thread available from the thread pool.
NOTE : Logging is not my problem but I want to be able to see how many tasks/jobs are waiting to be processed by a worker thread, is there anyway to see the length of the waiting queue (not the thread pool ) inside executor service?
I have no idea how to implement this thing.
Upvotes: 18
Views: 16193
Reputation: 1
I'd suggest keeping a counter of the tasks added to the pool and a counter of the tasks completed/started by the pool. Simple and works with any threading paradigm.
Upvotes: 0
Reputation: 2073
Just as a suggestion use ThreadPoolExecutor instead of ExecutorService. You can take advantage of the blocking queue present in the ThreadPoolExecutor class. This would give you the count of threads waiting.
Also ThreadPoolExecutor class is having methods to get the count of submitted tasks and executed task.
Refer the
Hope this helps
Upvotes: 0
Reputation: 10051
If you can assume that the ExecutorService
implementation used by your server is ThreadPoolExecutor
, then you can use the method getQueue()
that returns the number of tasks that have not been assigned to a Worker
yet.
/**
* Returns the task queue used by this executor. Access to the
* task queue is intended primarily for debugging and monitoring.
* This queue may be in active use. Retrieving the task queue
* does not prevent queued tasks from executing.
*
* @return the task queue
*/
public BlockingQueue<Runnable> getQueue() {
return workQueue;
}
So you can run something like this:
if(LOGGER.isDebugEnabled()) {
LOGGER.debug(String.format("Pending tasks: %d", executor.getQueue().size()));
}
Upvotes: 4
Reputation: 100139
The ThreadPoolExecutor
constructor accepts a BlockingQueue
parameter which is the Queue
implementation used to store the waiting jobs. You can request this queue using getQueue()
method, then check the size of the queue:
System.out.println("Number of waiting jobs: "+executor.getQueue().size());
Note that this method is not available in the ExecutorService
interface, thus it's better to construct the ThreadPoolExecutor
explicitly instead of using Executors.newFixedThreadPool
and friends:
ThreadPoolExecutor executor = new ThreadPoolExecutor(nThreads, nThreads,
0L, TimeUnit.MILLISECONDS,
new LinkedBlockingQueue<Runnable>());
While Executors.newFixedThreadPool
in OpenJDK/OracleJDK does the same, it's not specified, thus using (ThreadPoolExecutor)Executors.newFixedThreadPool(nThreads)
may cause ClassCastException
in future Java versions or alternative JDK implementations.
Upvotes: 23