39 points · by fogus · 16y ago · 33 comments
I challenge anyone to gain an understanding of volatile variables from his description. His example of volatile vs. synchronised contains an example of incorrect code but not an example of how it should be fixed, and doesn't even contain a particularly clear explanation of why it's bad.
The explanation of the bytecode generated for a synchronised method compared to a synchronised block is curious if you're into bytecode but totally useless for a working programmer, and offers no guidance on choosing between them. Worse, it actually makes it sound like the option you almost certainly don't want (the synchronised method) is somehow more efficient than the almost universally better method (synchronising on a lock object, which provides much better lock granularity, prevents deadlock in the case of some external object claiming your object lock and generally makes lock ownership much clearer and easier to track).
Finally, his example of the AtomicReferenceFieldUpdater ignores the javadoc (This class is designed for use in atomic data structures in which several reference fields of the same node are independently subject to atomic updates) and proceeds to show a contrived example of a compareAndSet that is really only a more confusing set, and doesn't actually achieve anything at all.
There are much better sources to learn this stuff if you really need to know it, starting with Java Concurrency In Practice - no Java programmer should be writing concurrent code without having read the first 7 chapters of this.
The explanation of volatile is woeful, to say the least. He doesn't even mention the main issue that volatile addresses (the only one?): visibility. Anyone really interested in this should read the following article by the author of JCIP: http://www.ibm.com/developerworks/java/library/j-jtp06197.ht...
I liked the section regarding ThreadLocal though (an example would've helped a bit).
Does anyone know the limits of the JVM for multithreading? e.g. Max number of cores
A few years back I did some comparisons between Java and Erlang with respect to the number of threads the system could handle. Somewhere around a thousand threads was sufficient to make the JVM go very slowly.
Also, were these threads proper OS threads, or more lightweight coroutines?
Thanks
I haven't been doing Java for a while now, but I hope that the situation has become better. The Erlang model of starting threads for all the tasks to do is very alluring once one gets used to it :)
> Also, were these threads proper OS threads, or more lighweight coroutines?
Exactly, it's apples to oranges in terms of what's happening underneath. Erlang has its own scheduler, whereas Java threads are system threads.
http://java.sun.com/docs/books/jls/third_edition/html/classe...
http://stackoverflow.com/questions/417285/equivalent-code-fo...
Supposing you want to synchronize only one part of your method, which approach would cause the JVM to do less work: putting that part in a synchronized block (thus generating extra bytecode instructions) or extracting it into a synchronized method (thus generating an additional call)?
Not that it's really important -- micro-optimizations like that are silly -- but it does make me curious.
I'm no Java expert, but the reason you'd synchronize a smaller block of code, or use any finer grain lock, rather than a whole method is that you'd have a shorter time to wait if many threads are competing for the lock. You'd increase utilization.
Lines of bytecode doesn't really tell you anything, especially since the synchronized method is a stub that does nothing. Not only that, but the synchronized method does all its synchronization work implicitly; the JVM still has to acquire the lock somehow, it's just not written in the bytecode.
What really matter is what happens when the bytecode is compiled to native code, so you could see the synchronized method call overhead. If the article analyzed that, it would be helpful.