单例模式精讲
程序员文章站
2024-01-20 12:35:40
...
源码已经上传在我的码云
有问题或者有更好的实现、欢迎给我留言、不胜感激
一个最普通的写法
网上最常见的一种写法
public class Single1 {
private static Single1 instance;
private Single1() {
}
public static Single1 getInstance() {
if (instance == null) {
instance = new Single1();
}
return instance;
}
}
发现问题
这种写法在单线程或者自己写测试的时候 一般不会遇到问题,但是在服务器遇到高并发的情况下 则会出现问题。
假如说 构造方法的耗时比较长
private Single1() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
new Thread("线程1") {
public void run() {
System.out.println(Single1.getInstance());
};
}.start();
new Thread("线程2") {
public void run() {
System.out.println(Single1.getInstance());
};
}.start();
}
测试的时候 便会发现这是俩不同的对象
解决问题
private static Single instance;
private Single() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static Single getInstance() {
if (instance == null) {
System.out.println(Thread.currentThread().getName() + "判断 == null");
synchronized (Single.class) {
System.out.println(Thread.currentThread().getName() + " 开始加锁");
instance = new Single();
System.out.println(Thread.currentThread().getName() + " 释放锁");
}
}
return instance;
}
public static void main(String[] args) {
new Thread("线程1") {
public void run() {
System.out.println(Single.getInstance());
};
}.start();
new Thread("线程2") {
public void run() {
System.out.println(Single.getInstance());
};
}.start();
}
最开始想的解决办法是加锁
但是发现仍然是俩不同的对象
线程1判断 == null
线程1 开始加锁
线程2判断 == null
线程1 释放锁
线程2 开始加锁
com.test.Single@1ec599cd
线程2 释放锁
com.test.Single@719e4b38
通过日志可以发现,加锁创建对象的时候,线程2已经判断==null了
继续改进
public class Single {
private static Single instance;
private Single() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static Single getInstance() {
if (instance == null) {
System.out.println(Thread.currentThread().getName() + "判断 == null");
synchronized (Single.class) {
System.out.println(Thread.currentThread().getName() + " 开始加锁");
if (instance == null) {
instance = new Single();
}
System.out.println(Thread.currentThread().getName() + " 释放锁");
}
}
return instance;
}
public static void main(String[] args) {
new Thread("线程1") {
public void run() {
System.out.println(Single.getInstance());
};
}.start();
new Thread("线程2") {
public void run() {
System.out.println(Single.getInstance());
};
}.start();
}
}
这时候发现正常了
线程1判断 == null
线程1 开始加锁
线程2判断 == null
线程1 释放锁
线程2 开始加锁
线程2 释放锁
com.test.Single@52bd51dc
com.test.Single@52bd51dc
不太友好的写法3
/**
* 不太好的写法3, 虽然没有bug 但是每次getInstance的时候都需要加锁判断
*
* @author coffee<br/>
* 2018年1月18日下午12:53:23
*/
public class Single3 {
private static Single3 instance;
private Single3() {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public static Single3 getInstance() {
synchronized (Single3.class) {
System.out.println(Thread.currentThread().getName() + "判断 == null");
if (instance == null) {
System.out.println(Thread.currentThread().getName() + " 开始加锁");
instance = new Single3();
System.out.println(Thread.currentThread().getName() + " 释放锁");
}
}
return instance;
}
public static void main(String[] args) {
new Thread("线程1") {
public void run() {
System.out.println(Single3.getInstance());
};
}.start();
new Thread("线程2") {
public void run() {
System.out.println(Single3.getInstance());
};
}.start();
}
}