That seems contradictory because once you exit the synchronized block everything should be visible.
Suma Rangaraj wrote:1) so what is the real issue with double checked locking on single vs multi-processor CPUs? How is the new memory model different?
2) if a thread 1 is executing a synchronized block and Thread 2 preempts thread 1, does it mean thread 1 will have to forcibly exit the synchronized block in between?
No. Once a thread is inside a synchronized block, it will not release the lock unless it calls the wait() method on the object it is synchronized on.
Pat Farrell wrote:And of course, all this is really about the Singleton Pattern, which is evil. See many other threads.
Stephan van Hulst wrote:Using a volatile variable would defeat the point of using double checked locking in the first place, wouldn't it? You're trading improved performance by avoiding a synchronization for decreased performance by using a volatile variable. So you end up with roughly the same performance, but less readability.