gpt4 book ai didi

java - 不同类型的单例模式

转载 作者:搜寻专家 更新时间:2023-11-01 01:25:26 25 4
gpt4 key购买 nike

我正在学习基本的软件设计模式。

单例类的基本实现是这样写的:

    public class MyObject{

private volatile static MyObject obj;

private MyObject(){/*Do some heavy stuff here*/}

public static synchronized MyObject getInstance(){
if(obj==null)
obj=new MyObject();
return obj;
}
}

但据我所知,调用同步方法可能很繁重。

前段时间我红了一本书介绍了这种Singleton类的实现方式:

public class MyObjectHolder {

private volatile static Supplier<MyObject> myObjectSupplier = () -> createMyObj();
//myObjectSupplier is changed on the first 'get()' call

public static MyObject getMyObject(){
return myObjectSupplier.get();
}

private static synchronized MyObject createMyObj(){
class MyObjectFactory implements Supplier<MyObject> {
private final MyObject clockTimer = new MyObject();
public MyObject get() { return clockTimer; }
}


if(!MyObjectFactory.class.isInstance(myObjectSupplier)) {
myObjectSupplier = new MyObjectFactory();
}
return myObjectSupplier.get();
}

public static class MyObject{

private MyObject(){
/*Do some heavy stuff here*/
}

public void someMethod(){
/* ... */
}
}

}


...


{
/*In main MyObject instantiation*/
MyObjectHolder.MyObject obj = MyObjectHolder.getMyObject();

}

现在,在完成对“createMyObj()”的第一次调用后,同步方法调用就没有沉重的负担,而且 if 也不会检查。

你觉得这样的实现有什么问题吗?

附言。 MyObject 不必是 MyObjectHold 的内部类,但我认为它看起来不错。

最佳答案

[已更新] 另一个名为 Initialization on Demand Holder idiom 的解决方案:

public class SingletonObject {

private static final AtomicInteger INSTANCE_COUNT = new AtomicInteger();
private static final AtomicInteger INVOKE_COUNT = new AtomicInteger();

private static final class LazyHolder {

private static final SingletonObject INSTANCE = new SingletonObject();

}

private SingletonObject() {
System.out.println("new SingletonObject");
INSTANCE_COUNT.getAndIncrement();
}

public static SingletonObject getInstance() {
INVOKE_COUNT.getAndIncrement();
return LazyHolder.INSTANCE;
}

public static int getInstanceCount() {
return INSTANCE_COUNT.get();
}

public static int getInvokeCount() {
return INVOKE_COUNT.get();
}

}

证明它是线程安全的:

public static void main(String[] args) throws Exception {
int n = 1000;
List<Callable<SingletonObject>> invokers = new ArrayList<>();
for (int i = 0; i < n; i++) {
invokers.add(SingletonObject::getInstance);
}
ExecutorService es = Executors.newFixedThreadPool(n);
es.invokeAll(invokers);
es.shutdown();
System.out.println("Number of Instances = " + SingletonObject.getInstanceCount());
System.out.println("Number of Invokes = " + SingletonObject.getInvokeCount());
}

输出:

new SingletonObject
Number of Instances = 1
Number of Invokes = 1000

编辑(在 @Holger 的评论之后):

使用 Nested Holder Class 有些必要 延迟初始化 SingletonObject

public class SingletonObject {

private static final SingletonObject INSTANCE = new SingletonObject();

private SingletonObject() {
System.out.println("new SingletonObject");
}

public static SingletonObject getInstance() {
return INSTANCE;
}

public static void anotherStaticMethod() {
System.out.println("I don't need the SingletonObject Instance...");
}

}

那么如果有人调用 anotherStaticMethod() 会发生什么?

new SingletonObject
I don't need the SingletonObject Instance...

更新:

页面位于 WIKIPEDIA说:

The implementation of the idiom relies on the initialization phase of execution within the Java Virtual Machine (JVM) as specified by the Java Language Specification (JLS). When the class SingletonObject is loaded by the JVM, the class goes through initialization. Since the class does not have any static variables to initialize, the initialization completes trivially. The static class definition LazyHolder within it is not initialized until the JVM determines that LazyHolder must be executed. The static class LazyHolder is only executed when the static method getInstance is invoked on the class SingletonObject, and the first time this happens the JVM will load and initialize the LazyHolder class. The initialization of the LazyHolder class results in static variable INSTANCE being initialized by executing the (private) constructor for the outer class SingletonObject. Since the class initialization phase is guaranteed by the JLS to be serial, i.e., non-concurrent, no further synchronization is required in the static getInstance method during loading and initialization. And since the initialization phase writes the static variable INSTANCE in a serial operation, all subsequent concurrent invocations of the getInstance will return the same correctly initialized INSTANCE without incurring any additional synchronization overhead. This gives a highly efficient thread-safe "singleton" cache, without synchronization overhead; benchmarking indicates it to be far faster than even uncontended synchronization. However, the idiom is singleton-specific and not extensible to pluralities of objects (e.g. a map-based cache).

同时关注this .

关于java - 不同类型的单例模式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35116162/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com