Question

I am extending the ThreadPoolExecutor class. Inside it I want to set a member value in the beforeExecute (Thread t, Runnable r) and the afterExecute (Runnable r, Throwable t). I am not sure how to do so. Can anyone help me with that?

Was it helpful?

Solution

Presumably, you want to access an instance field of your Runnable instances. To access them you will need to downcast the Runnable to the actual class of your Runnable class, and then access the fields via the downcast reference. Or better still, make the fields private and access them via getter / setter calls on the reference.

This could be awkward if your Runnable is an anonymous inner class. In that case, you may need to turn it into a named class: either nested, inner or top level.

On the other hand, if you are talking about static fields or methods of the Runnable implementation class, you can just use them as you would normally ... provided that the access modifiers allow this.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top