单例模式
饿汉式
- 私有构造方法
- static 静态new一个对象返回
优点: 简单,线程安全
缺点:类加载的时候,new一个对象,如果不使用,就属于浪费内存
/**
* 饿汉模式
*/
public class EHan {
/*静态new独享*/
private static EHan instance = new EHan();、
/*私有构造参数*/
private EHan() {
}
public static EHan getInstance(){
return instance;
}
public static void main(String[] args) {
EHan instance = EHan.getInstance();
EHan instance2 = EHan.getInstance();
System.out.println(instance);
System.out.println(instance2);
}
}
饿汉式变种
public class Singleton {
private Singleton(){
}
public static enum SingletonEnum {
SINGLETON;
private Singleton instance = null;
private SingletonEnum(){
instance = new Singleton();
}
public Singleton getInstance(){
return instance;
}
}
}
饿汉式-静态内部类
public class Singleton {
// 静态内部类
private static class SingletonHolder {
private static final Singleton INSTANCE = new Singleton();
}
private Singleton (){}
public static final Singleton getInstance() {
return SingletonHolder.INSTANCE;
}
}
懒汉式1.0版本
- 私有构造方法
- return实例的时候,判断是否null 再new对象
优点:1.使用的时候,在创建对象,节省资源
缺点:1.线程不安全
import sun.nio.ch.ThreadPool;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadPoolExecutor;
public class Lanhan {
private static Lanhan instance;
/*私有构造函数*/
private Lanhan() {
}
/*返回实例*/
public static Lanhan getInstance(){
/*判断实例是否为null*/
if (null == instance) {
instance = new Lanhan();
}
return instance;
}
public static void main(String[] args) {
for (int i = 0; i < 50000; i++) {
Runnable runnable = ()->{
System.out.println(Lanhan.getInstance());
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
};
Thread thread = new Thread(runnable);
thread.start();
}
}
}
懒汉式2.0
- 私有构造方法
- return实例的时候,使用sysnchronized,判断是否null 再new对象
优点:1.使用的时候,在创建对象,节省资源,线程安全
缺点:1.性能不好
public class Lanhan2 {
private static Lanhan2 instance;
/*私有构造函数*/
private Lanhan2() {
}
/*返回实例 添加同步关键词*/
public synchronized static Lanhan2 getInstance(){
/*判断实例是否为null*/
if (null == instance) {
instance = new Lanhan2();
}
return instance;
}
public static void main(String[] args) {
for (int i = 0; i < 50000; i++) {
Runnable runnable = ()->{
System.out.println(Lanhan2.getInstance());
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
};
Thread thread = new Thread(runnable);
thread.start();
}
}
}
懒汉式3.0 双检锁
- 私有构造方法
- return实例的时候,使用sysnchronized锁住类对象,判断是否null 再new对象
优点:1.使用的时候,在创建对象,节省资源,线程安全
缺点:1.性能不好
/**
* 双检锁
*/
public class DoubleCheck {
private DoubleCheck() {
}
/**防止指令重排**/
private volite static DoubleCheck instance;
public static DoubleCheck getInstance(){
if (null == instance) {
synchronized (DoubleCheck.class){
if (null == instance){
instance=new DoubleCheck();
}
}
}
return instance;
}
public static void main(String[] args) {
DoubleCheck instance = DoubleCheck.getInstance();
DoubleCheck instance2 = DoubleCheck.getInstance();
System.out.println(instance);
System.out.println(instance2);
}
}
❤推荐方式 枚举
优点:1.使用的时候,在创建对象,节省资源,线程安全
public class Singleton {
private Singleton(){
}
public static enum SingletonEnum {
SINGLETON;
private Singleton instance = null;
private SingletonEnum(){
instance = new Singleton();
}
public Singleton getInstance(){
return instance;
}
}
}