栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

java设计模式(单例模式)

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

java设计模式(单例模式)

设计模式 创建型模式 单例模式

单例模式的八种方式

  1. 饿汗式(静态常量)

  2. 饿汉式(静态代码块)

  3. 懒汉式(线程不安全)

  4. 懒汉式(线程安全,同步方法)

  5. 懒汉式(线程不安全,同步代码块)

  6. 双重检查

  7. 静态内部类

  8. 枚举


饿汉式(静态常量)

public class SingletonTest1 {
	public static void main(String[] args) {
		// 测试
		for (int i = 0; i < 10; i++) {
			new Thread(() -> {
				Singleton singleton = Singleton.getInstence();
				System.out.println("饿汗式(静态变量):" + Thread.currentThread().getName() + ":" + singleton.hashCode());
			}).start();
		}
	}

}

// 饿汗式(静态变量)
class Singleton {

	// 1.构造器私有化,外部不能new
	private Singleton() {

	}
	
	// 2.本类内部创建对象实例
	private final static Singleton instence = new Singleton();
	
	// 3.提供一个公有的静态方法,返回实例对象
	public static Singleton getInstence() {
		return instence;
	}
}

饿汉式(静态代码块)

public class SingletonTest02 {
    public static void main(String[] args) {
    	Singleton02 singleton1 = Singleton02.getInstence();
    	Singleton02 singleton2 = Singleton02.getInstence();
        System.out.println(singleton1 == singleton2); // true
        System.out.println(singleton1.hashCode());
        System.out.println(singleton2.hashCode());
    }

}

// 饿汗式(静态代码块)
class Singleton02{
	
    // 1.构造器私有化,外部不能new
    private Singleton02(){

    }
    
    // 2.本类内部创建对象实例
    private static Singleton02 instence;
    
    // 在静态代码块执行时,创建单例对象
    static {
        instence = new Singleton02();
    }
    
    public static Singleton02 getInstence(){
        return instence;
    }

}

懒汉式(线程不安全)

public class SingletonTest03 {
    public static void main(String[] args) {
    	
    	for (int i = 0; i < 15; i++) {
			new Thread(()->{
				Singleton03 singleton1 = Singleton03.getInstance();
				System.out.println(Thread.currentThread().getName()+":"+singleton1.hashCode());
			}).start();
		}
    	
    }

}

// 懒汉式(线程不安全)
class Singleton03{
	private static Singleton03 singleton;
	private Singleton03(){
		
	}
	
	public static Singleton03 getInstance() {
		if (null == singleton) {
			singleton = new Singleton03();
		}
		return singleton;
	}
}

懒汉式(线程安全,同步方法)

public class SingletonTest04 {
    public static void main(String[] args) {
    	
    	for (int i = 0; i < 15; i++) {
			new Thread(()->{
				Singleton04 singleton1 = Singleton04.getInstance();
				System.out.println(Thread.currentThread().getName()+":"+singleton1.hashCode());
			}).start();
		}
    	
    }

}

// 懒汉式(线程安全,同步方法)
class Singleton04{
	private static Singleton04 singleton;
	private Singleton04(){
		
	}
	
	public static synchronized Singleton04 getInstance() {
		if (null == singleton) {
			singleton = new Singleton04();
		}
		return singleton;
	}
}

懒汉式(线程不安全,同步代码块)

public class SingletonTest05 {
	public static void main(String[] args) {

		for (int i = 0; i < 15; i++) {
			new Thread(() -> {
				Singleton05 singleton1 = Singleton05.getInstance();
				System.out.println(Thread.currentThread().getName() + ":" + singleton1.hashCode());
			}).start();
		}

	}

}

// 懒汉式(线程不安全,同步代码块)
class Singleton05 {
	private static Singleton05 singleton;

	private Singleton05() {

	}

	public static Singleton05 getInstance() {

		if (null == singleton) {
			synchronized (Singleton05.class) {
				singleton = new Singleton05();
			}
		}

		return singleton;
	}
}

双重检查

public class SingletonTest06 {
	public static void main(String[] args) {

		for (int i = 0; i < 15; i++) {
			new Thread(() -> {
				Singleton06 singleton1 = Singleton06.getInstance();
				System.out.println(Thread.currentThread().getName() + ":" + singleton1.hashCode());
			}).start();
		}

	}

}

// 双重检查
class Singleton06 {
	private static Singleton06 singleton;
	
	private Singleton06() {

	}

	public static Singleton06 getInstance() {

		if (singleton == null) {
			synchronized (Singleton06.class) {
				if(singleton == null) {
					singleton = new Singleton06();
				}
			}
		}

		return singleton;
	}
}

静态内部类

public class SingletonTest07 {
	public static void main(String[] args) {

		for (int i = 0; i < 15; i++) {
			new Thread(() -> {
				Singleton07 singleton1 = Singleton07.getInstance();
				System.out.println("静态内部类:"+Thread.currentThread().getName() + ":" + singleton1.hashCode());
			}).start();
		}

	}

}

// 静态内部类
class Singleton07 {
	
	private static class SingletonInstance{
		private static final Singleton07 INSTANCE = new Singleton07();
	}

	private Singleton07() {

	}

	public static Singleton07 getInstance() {
		return SingletonInstance.INSTANCE;
	}
}

枚举

public class SingletonTest08 {
	public static void main(String[] args) {

		for (int i = 0; i < 15; i++) {
			new Thread(() -> {
				Singleton08 instance = Singleton08.INSTANCE;
				instance.sayHello();
				System.out.println("枚类:"+Thread.currentThread().getName() + ":" + instance.hashCode());
			}).start();
		}

	}

}

// 枚举,可以实现单例
enum Singleton08{
	INSTANCE;
	public void sayHello() {
		System.out.println("hello...");
	}
}

单例模式注意事项和细节说明

  1. 单例模式保证了系统内存中该类只存在一个对象,节省了系统资源,对于一些需要繁琐创建销毁的对象,使用单例模式可以提供系统性能
  2. 当想实例化一个单例类的时候,必须使用相应的获取对象的方法,而不是使用new
  3. 构造函数是私有的
暑期编程PK赛 得CSDN机械键盘等精美礼品!
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1016290.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号