Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!npeer02.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.com!postnews.google.com!u6g2000vbg.googlegroups.com!not-for-mail From: Saxo Newsgroups: comp.lang.java.programmer Subject: Question whether a problem with race conditions exists in this case Date: Wed, 14 Dec 2011 09:07:34 -0800 (PST) Organization: http://groups.google.com Lines: 62 Message-ID: <8bbbbee3-adcc-4f28-aff5-2e230b047401@u6g2000vbg.googlegroups.com> NNTP-Posting-Host: 81.200.198.20 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: posting.google.com 1323882454 23977 127.0.0.1 (14 Dec 2011 17:07:34 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Wed, 14 Dec 2011 17:07:34 +0000 (UTC) Complaints-To: groups-abuse@google.com Injection-Info: u6g2000vbg.googlegroups.com; posting-host=81.200.198.20; posting-account=QuOrbQoAAADk1Ytmd4kkfWK7_Nh1pnw_ User-Agent: G2/1.0 X-HTTP-Via: 1.1 modpragw-108v.linux.rz.db.de:8080 (squid/2.7.STABLE9), 1.0 izumi (squid/3.1.15) X-Google-Web-Client: true X-Google-Header-Order: ARLEUHCVFNK X-HTTP-UserAgent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.30729; .NET CLR 1.1.4322; .NET CLR 3.5.30729; .NET4.0C; .NET4.0E),gzip(gfe) Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:10728 I have a class Node as displayed below that holds a new value and the previous value. Which one applies is determined by the value of useNewValue. In my application there is a list of such nodes where for each node the current value is replaced with a new one. The new values should become effective for the entire list all >at once< through an atomic change of useNewValue in every node. For that purpose, when the new value is set, for every node in the list the same AtomicBoolean instance is passed on to setNewValue(...), which is stored in useNewValueParam. When the commit is done, the value of this instance of AtomicBoolean is changed to true (this way the thread doing the commit does not have to enter the synchronized block as all the other threads calling aNode.get) and thus the new value of every node becomes visible at once to every thread calling aNode.get(). public class Node { AtomicBoolean useNewValue = new AtomicBoolean(false); private Object newValue = null; private Object previousValue = null; private Object lock = new Object(); public Object get() { synchronized(lock) { if(useNewValue.get()) // 1 return newValue; // 2 return previousValue; // 3 } } public void setNewValue(Object newValue, AtomicBoolean useNewValueParam) { synchronized(lock) { if(this.useNewValue.get()) previousValue = this.newValue; this.newValue = newValue; // useNewValueParam is allways set to false when setNewValue is called this.useNewValue = useNewValueParam; } } } At the same time there is never more than one thread iterating over the node list calling setNewValue. This is made sure through serialization of threads that want to iterate over the list calling setNewValue. Serialization of threads is a bit crude, but not relevant at the moment for the discussion of the problem described here. My question is now whether this approach is free of race conditions or starvation issues if implemented as described above. I have some doubts whether everything is fine here as useNewValue is changed by the commit thread by reference without entering synchronized(lock) { ... }. So is everything still fine if a context switch happens between line 1 and 2 or between line 1 and 3? It would be for sure if the commit thread entered the synchronized(lock) { ... } block, but it does not (changing to the new values all at once wouldn't be possible otherwise). I would be thankful if someone could look over this and tell me her/ his opinion. Thanks al lot, Oliver