Subj : Re: posix and lock-free algorithms To : comp.programming.threads From : David Holmes Date : Wed Aug 10 2005 03:30 pm "Joe Seigh" wrote in message news:8q6dnZ2dnZ0I8bSlnZ2dnVmzb9-dnZ2dRVn-052dnZ0@comcast.com... > You can see what's Java currently says about this sort of optimization. > I know an early Java compiler optimized out empty synchronized blocks and > later had to fix it. But I don't know if it's allowed with current > Java semantics. synchronization on a thread-local object can be completely optimized away eg: synchronized(new Object()) { doSomething(); } can be transformed to just: doSomething(); An empty synchronized block on a non-thread-local object cannot be completely optimized away, but must preserve the acquire/release semantics guaranteed by the synchronized block. (In overly simplistic terms, the locking can be elided but memory barriers might still be needed.) Cheers, David Holmes .