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...)