单例模式 线程安全、性能最高

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;


public class Singletion {
private static volatile Singletion single=null;
private static Lock lock =new ReentrantLock();
private Singletion(){

}
public static Singletion getSingle(){
if(single==null){
lock.lock();
if(single==null){
single=new Singletion();
}
lock.unlock();
}
return single;
}
}

你可能感兴趣的:(设计模式)