Singleton pattern

Singleton: Ensure a class only has one instance and provide a global point of access to it.

code implementation

public class Singleton {
private static Singleton uniqueInstance;

private Singleton() {}

public static Singleton getInstance() {
if (uniqueInstance==null)
uniqueInstance=new Singleton();
return uniqueInstance;
}

}


We have a problem

The new Singleton code was running fine. The only thing we can think of is that we just added some optimizations to the Controller that makes use of multiple threads.

public class Singleton {
private static Singleton uniqueInstance;
private Singleton() {}
public static synchronized Singleton getInstance() {
if (uniqueInstance ==null)
uniqueInstance=new Singleton();
return uniqueInstance;
}
}


I agree this fixes the problem. but synchronization is expensive.
it's actually a little worse than you make out; the only time synchronization is relevant is the first time through this method.

Can we improve multithreading?

1. Do nothing if the performance of getInstance() isnt critical to your application.

If calling the getInstance() method isnt causing substantial overhead for your application. forget about it. Keep in mind that synchronizing a method can decrease performance by a factor of 100, so if a high traffic part of your code begins using getInstance(), you may have to reconsider.

2 move to an eagerly created instance rather than a lazily created one.


if your application always creates and uses an instance of the Singleton or the overhead of creation and runtime aspects of Singleton are not oncrous, you may want to create your singleton eagerly.

public class Singleton {
private static Singleton uniqueInstance = new Singleton();
private Singleton() {}
public static Singleton getInstance() {
return uniqueInstance;
}
}


Using this approach, we rely on the JVM to create the unique instance of the Singleton when the classs is loaded. The JVM guarantees that the instance will be created before any thread access the static uniqueInstance variable.

3 Use "double-checked locking" to reduce the use of synchronization in getInstance()

With double-checked locking, we first check to see if an instance is created, and if not, then we synchronize. This way, we only synchronize the first time through, just what we want.

Let's check out the code:

public class Singleton {
private volatile static Singleton uniqueInstance;

private Singleton() {}
public static Singleton getInstance() {
if (uniqueInstance==null) {
synchronized (Singleton.class) {
if (uniqueInstance==null)
uniqueInstance=new Singleton();
}
}
return uniqueInstance;
}
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值