54 points · by heapster · 13y ago · 23 comments
That was a fun one to explain in office hours.
"No no, it's not enough to just put this before that, you have to establish a Happens-Before relationship."
http://stackoverflow.com/questions/16159203/why-does-this-ja...
There are definitely some surprising outcomes from the JMM, but I'm pretty sure this isn't one of them. If I recall correctly, starting a thread automatically synchronizes the current thread with the first thing the child thread does, without any explicit synchronization. So anything done prior to that point in the parent thread will be visible in the child.
Maybe the child thread was created some time before it was started, and the initialization happened some time between those events. It was an example that a friend was working for a class, and as they often do, came to me for help on why it wasn't behaving as expected.
I looked at it and I could see the problem, "you don't have any locks shared between parent and child." We added a lock, synchronized it once in each thread, and the problem went away. That's definitely a surprising outcome for anyone who is accustomed to procedural programming.
FYI, the relevant section of the JLS is 17.4.4: "An action that starts a thread synchronizes-with the first action in the thread it starts." [1].
[1] http://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html...
It could be just me but I think the author should've mentioned alternative concurrent programming models available for JVM such as Akka.
At a bare minimum the changes to the location of the head/tail of a queue will need to be visible to multiple threads requiring volatile variables.
The java concurrent queues don't actually use synchronization blocks, they use native CAS operations but thinking that akka removes shared thread state entirely is dangerous.
The costs of synchronised blocks is high on the JVM. They defer to the operating system for thread scheduling, they enforce a memory barrier meaning all data is flushed out to RAM rather than CPU caches, and multithreaded code dirties CPU caches, reducing performance.
More server side code should be single threaded than not in my opinion.
That said, I completely agree. Many times making a single threaded system faster will improve performance vs making it parallel.
A little old but Oracle JRockit: The Definitive Guide is a pretty good book on the JRockit JVM.
Or is this book applicable beyond Java?