4 ms·
The article really fails here, this is a horrible example and horrible reasoning. I'm no Java expert, but the reason you'd synchronize a smaller block of code,
by drblast 16y ago
The article really fails here, this is a horrible example and horrible reasoning.
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.