N
neuneudr
Hi all,
On Oreilly's website there's a column about implementing
multi-threaded algoriths, with an example of quicksort in Java.
I'm scratching my head on someting...
May Column: Multi-threaded Algorithm Implementations
http://broadcast.oreilly.com/2009/06/may-column-multithreaded-algor.html
I read the column and thought "that ain't working", then downloaded
the code.
Here's the array to be sorted:
/** Elements to be sorted. */
@SuppressWarnings("unchecked")
final Comparable[] ar;
So it's a regular array.
I fail to see what guarantees are made in the sorting code
that swaps made by spawned threads are visible to someone
who would be observing the [].
Say we have 8,7,6,5,4,3,2,1 and the code makes the swaps
in the array and as now 4,3,2,1,8,7,6,5 and asks to two threads
to take care respectively of the 4,3,2,1 part and the 8,7,6,5 part.
I agree that the spawned threads are going to see correctly what
they should. But what guarantee are there that an observer will
indeed see the array sorted at the end?
To me to work the code needs to be changed, the following
doesn't seem correct:
final Comparable[] ar;
I'd replace it with:
final AtomicReferenceArray<Comparable> ar;
To me you can't simply spawn thread and pass them
a reference to an [] and indexes and tell it "go ahead,
swap and re-arrange the [] as you like" and hope that
changes are going to be visible. Java makes no such
guarantees right!?
While a AtomiceReferenceArray it's guaranteed to work,
but it incurs a performance hit compare to the [] version.
But then I guess I don't understand anything about the Java
memory model nor about the code posted (there a tiny code.zip
file, once you keep only the .java files related to quicksort
it's tiny).
Thoughts?
On Oreilly's website there's a column about implementing
multi-threaded algoriths, with an example of quicksort in Java.
I'm scratching my head on someting...
May Column: Multi-threaded Algorithm Implementations
http://broadcast.oreilly.com/2009/06/may-column-multithreaded-algor.html
I read the column and thought "that ain't working", then downloaded
the code.
Here's the array to be sorted:
/** Elements to be sorted. */
@SuppressWarnings("unchecked")
final Comparable[] ar;
So it's a regular array.
I fail to see what guarantees are made in the sorting code
that swaps made by spawned threads are visible to someone
who would be observing the [].
Say we have 8,7,6,5,4,3,2,1 and the code makes the swaps
in the array and as now 4,3,2,1,8,7,6,5 and asks to two threads
to take care respectively of the 4,3,2,1 part and the 8,7,6,5 part.
I agree that the spawned threads are going to see correctly what
they should. But what guarantee are there that an observer will
indeed see the array sorted at the end?
To me to work the code needs to be changed, the following
doesn't seem correct:
final Comparable[] ar;
I'd replace it with:
final AtomicReferenceArray<Comparable> ar;
To me you can't simply spawn thread and pass them
a reference to an [] and indexes and tell it "go ahead,
swap and re-arrange the [] as you like" and hope that
changes are going to be visible. Java makes no such
guarantees right!?
While a AtomiceReferenceArray it's guaranteed to work,
but it incurs a performance hit compare to the [] version.
But then I guess I don't understand anything about the Java
memory model nor about the code posted (there a tiny code.zip
file, once you keep only the .java files related to quicksort
it's tiny).
Thoughts?