java - compare AtomicInteger without setting it -
how can compare atomicinteger
given int
value without setting atomicinteger
new value? don't see such method in atomicinteger
.
by time get() returns, value in atomicinteger may updated new value... may stale value
by definition, if value in atomic integer gets updated right after call int = atomic.get()
have stale value in i
. that's how concurrent programs work - when consult value, know may have changed.
what atomicinteger::get
guarantees when call it, latest value available @ time of call. guarantee not have plain int
example.
in other words, imagine program below:
if (atomic.get() == 0) print("zero");
even if had sort of comparebutnotset
method, not because time reach print statement value of atomic may have changed anyway...
if need make sure print called if value still 0, need synchronize (or use lock around) whole block.
Comments
Post a Comment