两种常见的单例模式
静态内部类单例模式
/*
* 静态内部类单例模式
*/
public class Singleton {
private Singleton() {
}
private static final class SingletonHolder {
private static final Singleton INSTANCE = new Singleton();
}
public static Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Singleton.getInstance().hashCode());
}
}, "t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Singleton.getInstance().hashCode());
}
}, "t2");
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Singleton.getInstance().hashCode());
}
}, "t3");
t1.start();
t2.start();
t3.start();
}
}
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class InnerSingleton {
private InnerSingleton() {
}
private static final class InnerSingletonHandle {
private static final InnerSingleton INSTANCE = new InnerSingleton();
}
public static InnerSingleton getInstance() {
return InnerSingletonHandle.INSTANCE;
}
public static void main(String[] args) {
int num = 10;
ExecutorService executorService = Executors.newCachedThreadPool();
for (int i = 0; i < num; i++) {
executorService.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + ": " + InnerSingleton.getInstance().hashCode());
}
});
}
executorService.shutdown();
}
}
运行结果
```
pool-1-thread-1: 991873903
pool-1-thread-2: 991873903
pool-1-thread-3: 991873903
pool-1-thread-4: 991873903
pool-1-thread-6: 991873903
pool-1-thread-7: 991873903
pool-1-thread-8: 991873903
pool-1-thread-10: 991873903
pool-1-thread-5: 991873903
pool-1-thread-9: 991873903
Process finished with exit code 0
</div>
### 双重校验单例模式
```java
/*
* 双重校验单例模式
*/
public class Singleton {
private static volatile Singleton instance;
private Singleton() {
}
public static Singleton getInstance() {
if (instance == null) {
synchronized (Singleton.class) {
if (instance == null) {
instance = new Singleton();
}
// instance = new Singleton();
}
}
return instance;
}
public static void main(String[] args) {
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Singleton.getInstance().hashCode());
}
}, "t1");
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Singleton.getInstance().hashCode());
}
}, "t2");
Thread t3 = new Thread(new Runnable() {
@Override
public void run() {
System.out.println(Singleton.getInstance().hashCode());
}
}, "t3");
t1.start();
t2.start();
t3.start();
}
}
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
public class DoubleCheckSingleton {
private DoubleCheckSingleton() {
}
private static volatile DoubleCheckSingleton INSTANCE;
public static DoubleCheckSingleton getInstance() {
if (INSTANCE == null) {
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (DoubleCheckSingleton.class) {
if (INSTANCE == null) {
INSTANCE = new DoubleCheckSingleton();
}
// INSTANCE = new DoubleCheckSingleton();
}
}
return INSTANCE;
}
public static void main(String[] args) {
int num = 10;
ExecutorService executorService = Executors.newCachedThreadPool();
for (int i = 0; i < num; i++) {
executorService.execute(new Runnable() {
@Override
public void run() {
System.out.println(Thread.currentThread().getName() + ": " + DoubleCheckSingleton.getInstance().hashCode());
}
});
}
executorService.shutdown();
}
}
运行结果
```
pool-1-thread-1: 405652733
pool-1-thread-4: 405652733
pool-1-thread-7: 405652733
pool-1-thread-2: 405652733
pool-1-thread-6: 405652733
pool-1-thread-5: 405652733
pool-1-thread-3: 405652733
pool-1-thread-8: 405652733
pool-1-thread-9: 405652733
pool-1-thread-10: 405652733
Process finished with exit code 0
</div>
### 最后总结
![](https://img2018.cnblogs.com/blog/1306719/201811/1306719-20181107155022590-664214116.png)
![](https://img2018.cnblogs.com/blog/1306719/201811/1306719-20181107155028084-1715481635.png)