inheritance - Making variable volatile in subclass, in Java -
i've come across following situation:
public class foo { private boolean valid; ... } public class concurrentfoo extends foo { ... }
since concurrentfoo
subclass used in multithreaded environment, opposed foo, not thread-safe, i'd boolean valid
be, instead, volatile boolean valid
, only in subclass.
the goal using volatile
avoid locks , synchronization, since seem unnecessary. there ever 2 updates variable in lifetime of object, , lots of (concurrent) reads.
of course, use synchronization solve problem in subclass, or implement other way, such there clearer distinction when using foo
, concurrentfoo
. instance:
public class concurrentfoo extends foo { // considered shadowing, when adding volatile? private volatile boolean valid; // or fresh name. private volatile boolean concurrentvalid; }
anyway, i curious know if possible modify non-access modifiers, such volatile
(well, except final
), in subclass.
if isn't possible, , think may not be, unless shadowing alternative considered, what simplest way overcome situation?
synchronization in subclass?
do tell, if more context needed.
should note performance of relevance in case.
i suspect not making volatile premature optimization. if don't need volatile, made volatile, wasting 3-10 nano-seconds. if call 100 times wasting micro-second (over life of component suspect). amount of time critical you?
Comments
Post a Comment