Double Checked Locking
2 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Sandeep_Shilawat
Posted On:   Monday, August 25, 2003 07:06 AM

This piece of code does not work, ( this is from one of the articles on DCL) public static Hook getInstance(){ if (initHolder.get() == null) { synchronized(s_instance){ if (s_instance == null) s_instance = new Hook(); initHolder.set(Boolean.TRUE); } } return s_instance; } It throws a NullPointer at synchronized(s_instance){ . Does anybody knows anything about this? This was a supposed be solution for, Singleton pattern broken by Java Memory Model. This solution solved the problem by using Double Checked Locking and uses ThreadLocal to ensure no pre-empting of threads occur !    More>>

This piece of code does not work,
( this is from one of the articles on DCL)

			
public static Hook getInstance(){
if (initHolder.get() == null) {
synchronized(s_instance){
if (s_instance == null)
s_instance = new Hook();
initHolder.set(Boolean.TRUE);
}
}
return s_instance;
}


It throws a NullPointer at synchronized(s_instance){
. Does anybody knows anything about this?


This was a supposed be solution for, Singleton pattern broken by Java Memory Model. This solution solved the problem by using Double Checked Locking and uses ThreadLocal to ensure no pre-empting of threads occur !

   <<Less

Re: Double Checked Locking

Posted By:   Benoit_Quintin  
Posted On:   Monday, August 25, 2003 12:02 PM

To add upon the last poster's answer, I remember reading that particular article, and had you bothered to read until the end, it states that

The JMM was the first attempt to define shared memory semantics in the context of the language specification for a general-purpose programming language. Unfortunately, it is quite complicated, poorly understood, and not consistently implemented across JVMs. Not surprisingly, the JMM's complexity resulted in JVM implementation errors and widespread misunderstandings among users -- such as the misperception of how synchronized works, which leads to unsafe idioms like DCL. While there are proposals on the table for a simpler memory model, it will be quite a while before we see such fundamental changes to the Java specification, if ever.
see http://www.javaworld.com/jw-02-2001/jw-0209-double.html

Re: Double Checked Locking

Posted By:   Nick_Maiorano  
Posted On:   Monday, August 25, 2003 10:48 AM

Sandeep,



I don't know where you got this piece of code but it is bugged. You can't synchronize on s_instance because it is null. That's why you are getting an error.



Until the java memory model is revised and/or standardized, there is no way to guarantee that this form of lazy initialization will work on every processor every time. So, save yourself some trouble and simply put a synchronized statement on the method rather than in the code. Your code should look like this:



public static synchronized Hook getInstance(){

if (s_instance == null)

s_instance = new Hook();


return s_instance;

}



This will work every time - even if somewhat less efficient.

About | Sitemap | Contact