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;
}
}
请教一下这两种方法的区别是什么,哪个更好?
Method 2 is better because method 1 uses concurrency and consumes performance.
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
Regarding the singleton mode, there are actually 5 ways:
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.
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.
Lazy mode of double check lock.
Lazy mode for static inner classes.
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
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.