Cheers, Roberto Perillo
SCJP, SCWCD, SCJD, SCBCD
Jack Nam wrote:Although the sequenceValue is thread-safe, but I wonder if the property is. It could be like a volatile array, not thread-safe for the element.
Mike Simmons wrote:I would say that the code show is thread-safe if used as shown here.
Mike Simmons wrote:However nothing seems to prevent other code from calling getNextValue() in an unsafe manner, without synchronizing.
Cheers, Roberto Perillo
SCJP, SCWCD, SCJD, SCBCD
Anupam Sinha wrote:I think that the code is not thread safe.
This simple looking line
is not thread safe. Java only guarantees a 32 bit to be atomic (unless otherwise made atomic). So nextVal should be made volatile.
Apart from this I am not able to understand this program much.
I am unable to understand how this program (barring the above reason) will be thread safe (if it is made to compile).
getNextval is not synchronized so even acquiring a lock on sequenceValue shouldn't matter much.
Anupam Sinha wrote:What if someone accesses sequenceValue.getNextVal(); directly without the lock?
Anupam Sinha wrote:Java only guarantees a 32 bit to be atomic (unless otherwise made atomic). So nextVal should be made volatile.
Consider Paul's rocket mass heater. |