How should I access class members in beforeExecute and afterExecute hooks in ThreadPoolExecutor?

StackOverflow https://stackoverflow.com/questions/23258643

سؤال

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?

هل كانت مفيدة؟

المحلول

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.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top