Reputation: 3502
I have a pretty complex piece of software that is heavily utilizing multi-threading to compute financial tick feeds and near the end of my market session, my thread pool queue starts to bloat and increase to the point where the system is no longer stable. To resolve this I need to get more data on how long Runnable's of different class types are taking to execute, or somehow flag the ones taking longer than x seconds to execute. I'm also trying to get insight if I am over allocating cores to VM's on my servers causing the JVM taking a while to obtain a core for thread execution.
Monitoring the queue size is pretty simple, but getting these other metrics I don't see any API's in Java that make this possible, any help would be appreciated, this is my last major blocker!
Upvotes: 4
Views: 2235
Reputation: 31299
There are a gazillion ways to do this, using many different libraries. You could use some AOP approach, or let Spring instrument your classes, use commercial solutions, etc.
If you're looking for a simple approach that you could implement by hand, then this is one.
You can subclass java.util.concurrent.ThreadPoolExecutor
and override two methods:
protected void beforeExecute(Thread t, Runnable r) { }
and
protected void afterExecute(Runnable r, Throwable t) { }
These methods don't do anything in their default implementation, but as the Javadoc says,
Method invoked prior to executing the given Runnable in the given thread. This method is invoked by thread {@code t} that will execute task {@code r}, and may be used to re-initialize ThreadLocals, or to perform logging.
In these methods you could collect your own timing information for the Runnables and log them to a file.
Upvotes: 5