Monitoring Java threadpool queue - How to know the queue size and how long a task spent there?
I have a threadpool constructed followingly:
int corePoolSize = 10;
int maximumPoolSize = 10;
long keepAliveTime = 0L;
TimeUnit unit = MILLISECONDS;
BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<T>();
ThreadFactory threadFactory = new MyCustomThreadFactory();
RejectedExecutionHandler rejectedExecutionHandler = new MyRejectedExecutionHandler();
ExecutorService executorService = new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, threadFactory, rejectedExecutionHandler);
I'm using it and it works fine but has poor observability. Things I would like to know which I currently don't are:
1. Each time a task is inserted to workQueue, which means that the pool is full, I'd like to know how big the queue is.
For that I wrapped the LinkedBlockingQueue in my own queue implementation where I execute some code if something is offered to queue.
2. Before a task is acutally started with, I'd like to know how long it stayed on the queue before that and how many parallel tasks are running at the same time.
I have not found a clean solution for that.
I'm wondering if there is some good practic on doing this stuff? Ideally I'd like some callbacks with this info, so I could decide what I'm goind to do with the metrics (log it, send it to some monitoring service, etc...)
For that I wrapped the LinkedBlockingQueue in my own queue implementation where I execute some code if something is offered to queue.
You can use ThreadPoolExecutor.getQueue() method and get a queue's size at any time. So you don't need to have own implementation of the queue.
Before a task is acutally started with, I'd like to know how long it stayed on the queue before that and how many parallel tasks are running at the same time.
Such problem could be solved only using an additional data structures because you need to know the time of task enqueuing and dequeuing. But, I believe, you don't need to know how long the particular task stayed in the queue. Actually you need to know some aggregated time in general. Just an idea, you can use size of queue and calculate time of invariant validity when the queue size is more then zero or something else. You could use beforeExecute() and afterExecute() callbacks in order to achieve a desirable functionality.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With