public void execute(Runnable command)
This command object contains the submitted object, but it seems to have been wrapped.
How can I access the submitted object from within a custom thread pool executor? Or is it not such a good idea to try and access the submitted object from inside a ThreadPoolExecutor’s before/after/execute methods?
Don’t use
execute, usesubmit, which returns aFuture, which is a handle to the task. Here’s some example code:Although you can’t access the task directly, you can still interact with it:
You can also interact with the service:
EDITED TO INCORPORATE COMMENTS:
If you want to do some logging, use an anonymous class to override the
afterExecute()method, like this:Override other methods as required.
Quick plug: IMHO, the bible for this subject is Java Concurrency in Practice – I recommend you buy it and read it.