Re: Synchronization Question
markspace wrote:
Knute Johnson wrote:
Each of the OPs array modifying threads would write to a common
volatile variable as the last action before terminating. Prior to
reading the array, the reading thread reads the common volatile
variable and all variables written to by the writing threads will be
now be visible to the reading thread.
Hmm, I didn't re-read the JLS, but off the top of my head this won't work.
Volatile variables only have a happens-before relationship for
themselves, i.e., only for reads/writes to that one variable. That's
why they are cheaper than regular synchronization, which has to flush
all writes.
There's no synchronization (happens-before) relationship between
different volatile variables, or for other read/writes in a thread.
I can only refer you to Java Concurrency in Practice, by Brian Goetz et
al. "The visibility effects of volatile variables extend beyond the
value of the volatile variable itself. When thread A writes to a
volatile variable and subsequently thread B reads that same variable,
the values of all variables that were visible to A prior to writing to
the volatile variable become visible to B after reading the volatile
variable."
--
Knute Johnson
email s/nospam/knute2009/
--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access