public class SingletonUnsafeDemo
{
public static void main( String[] args )
{
Thread t1 = new Thread()
{
public void run()
{
System.out.println( "UnsafeSingleton: " + UnsafeSingleton.getInstance() );
}
};
Thread t2 = new Thread()
{
public void run()
{
System.out.println( "UnsafeSingleton: " + UnsafeSingleton.getInstance() );
}
};
t1.start();
t2.start();
}
}
class UnsafeSingleton
{
static UnsafeSingleton instance;
public static UnsafeSingleton getInstance()
{
if ( instance == null )
{
Thread.yield(); // !!
instance = new UnsafeSingleton();
}
return instance;
}
}
Friday, November 19, 2010
Unsafe Singleton
Many mighy think, is there something called unsafe singleton? Singleton is the property, which lets just only one object floats at a time and there are no more than one object. This singleton program will create more than one object under multi threaded environment, which is totally against rationale.
Tuesday, August 10, 2010
Round VI
Round V
Round IV
Round III
Labels:
decorator,
dependency injection,
LRU cache,
volatile
Round II
synchronized methods vs synchronized blocks.
Thursday, July 15, 2010
My CSC Rendezvous....
Subscribe to:
Posts (Atom)