Memory Model (yeah, this again...)
Ken Gentle <[email protected]> Thu, 20 Nov 2008 08:33:36 -0500
| Newsgroups | gmane.comp.windows.devel.java.advanced |
|---|---|
| Message-ID | <LISTSERV%[email protected]> |
I have a debate going on StackOverflow over an answer to this question: http://stackoverflow.com/questions/300316/java-concurrency-scenario-do-i-= need-synchronization-or-not#300394 I'm 'Ken G' in that forum. My understanding of the problem/question: 1) A Single instance of a class with an instance variable (heap, IIRC) th= at is a reference to a HashMap - private visibility, not volatile. 2) Multiple threads containing "readers" that get the value of the HashMa= p reference via an unsynchronized accessor method . Threads exit, new th= reads get a HashMap reference as part of their startup via the method 3) The application refreshes with a new HashMap reference on some periodi= c basis. 4) It is OK if existing reader threads have references to an "old" HashMa= p as long as new threads get the reference to the "newest" HashMap. Given those conditions, my understanding is that no synchronization is re= quired for *new* reader threads to pick up the most recent value of the H= ashMap reference. With #4, there is no added value by synchronization or= declaring the private, defining reference "volatile". The dissenting opinion is that "volatile" is required (or some other barr= ier) to force the *application's* copy of the reference to be visible to = other threads. We've both been through section 17, and either we're not understanding it= or we're not agreeing on the requirement. Would ya'll please take a look and point out if/where we're incorrect? =3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D View archives and manage your subscription(s) at http://peach.ease.lsoft.com/archives