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

Popular posts from this blog

java - WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [/board/] in DispatcherServlet with name 'appServlet' -

html - Outlook 2010 Anchor (url/address/link) -

android - How to create dynamically Fragment pager adapter -