java单例模式(线程安全,效率高,双重推断)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java单例模式(线程安全,效率高,双重推断)相关的知识,希望对你有一定的参考价值。

这样的方法,在获取单利的时候,避免了线程锁,导致訪问该方法速度非常慢,
同是,防止了多线程同事房屋该方法就会产生多个实例的问题。


效率高。线程安全。


public class TestInstance{
private static TestInstance  instance;
public static TestInstance   getInstance{
    if(instance==null){
        synchronized(
TestInstance.class){

            if(instance==null){
            
instance=new TestInstance();
            }

    }
    return 
instance;
    }


}



}

以上是关于java单例模式(线程安全,效率高,双重推断)的主要内容,如果未能解决你的问题,请参考以下文章