java的单例模式的一个问题
伊谢尔伦
伊谢尔伦 2017-04-18 10:30:14
0
6
469

java单例模式下面两种方法有什么区别?

方法一:

public class SingleTon {

private static SingleTon singleTon;

private SingleTon(){
}

public synchronized  static SingleTon getSingleTon(){
    if(singleTon == null){
        singleTon = new SingleTon();
    }
    return singleTon;
}

}

方法二:

public class SingleTon {


private static SingleTon singleTon = new SingleTon();

private SingleTon(){
}

public synchronized static SingleTon getSingleTon(){
    return singleTon;
}

}

请教一下这两种方法的区别是什么,哪个更好?

伊谢尔伦
伊谢尔伦

小伙看你根骨奇佳,潜力无限,来学PHP伐。

reply all(6)
伊谢尔伦

Method 2 is better because method 1 uses concurrency and consumes performance.

Peter_Zhu

It’s lazy mode and hungry mode. Simply put, it’s lazy loading. I can’t say whether this kind of thing is good or not, it depends on the actual scene.

迷茫

Method 2 only prepares for concurrency, that is, taking into account the concurrent acquisition of singleton objects. Whether there will be concurrent access is decided by the user of this class.
These two types each have their own advantages and disadvantages. The first method is fast to load, but slower to obtain the singleton object, because if judgment is used, and the new object is retrieved during runtime. The second method is slower to load the class, but faster to obtain. Because static members are initialized when the class is loaded. At the same time, your second method does not need to be synchronized, because you just return a reference to the object and do not involve the new object

小葫芦

This is the two design methods of the singleton mode. Both use synchronization to prevent concurrency and thread safety issues. However, the first is a lazy man's design method and the second is a hungry man's design method. There is not much difference in the program itself. The timing of getting the object is just different

PHPzhong

Regarding the singleton mode, there are actually 5 ways:

  1. Lazy mode corresponds to your first code. The advantage is: delayed loading. The disadvantage is: lock synchronization is required every time you obtain an instance, which wastes resources.

  2. The Hungry Man mode corresponds to your second code. The advantage is: thread safety. The disadvantage is: loading as soon as it comes up, which wastes resources.

  3. Lazy mode of double check lock.

  4. Lazy mode for static inner classes.

  5. In Effective Java, it is recommended to use enum to implement singletons.

大家讲道理

The difference between the two is that method one is lazy loading. If this method is not called, no object will be generated, while the latter will generate an instance when loading the class, regardless of whether it is used or not.
The most typical lazy loading method should be like this

public static XXX getInstance(){
        if(xxx == null){
            synchronized(XXX.class){
                if(xxx == null){
                    xxx = new XXX();
                }
            }
        }
        return xxx;
    }

This saves time and does not create new objects.
In addition, if the concurrency situation is very high, you can use volatile to modify the instance.

---------------------I didn't look at your code carefully, so I'll add something----------

I found that you put a synchronization lock on the getInstance method. This is completely unnecessary. Synchronization is only required when it is first created. Subsequent synchronization is a complete waste of time.

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template