単例モード2種類の書き方
1248 ワード
//
class Singleton{
private static Singleton instance;
private static byte[] lock = new byte[0];
private Singleton(){
}
public static Singleton getInstance(){
if(instance==null){
synchronized(lock){
if(instance==null){
instance =new Singleton();
}
}
}
return instance;
}
}
二番目の書き方
class Singleton{
private static Singleton instance;
private static ReentrantLock lock =new ReentrantLock();
private Singleton(){}
public static Singleton getInstance(){
if(instance ==null){
lock.lock();
if(instance==null){
instance =new Singleton();
}
lock.unlock();
}
return instance;
}
}