美文网首页
设计模式:单例模式(4)

设计模式:单例模式(4)

作者: 谁家的猪 | 来源:发表于2019-07-24 08:29 被阅读0次

容器单例模式

代码演示

  1. 创建ContainerSingleton类
import org.apache.commons.lang3.StringUtils;

import java.util.HashMap;
import java.util.Map;

/**
 * @author lijiayin
 */
public class ContainerSingleton {
    private ContainerSingleton() {
        
    }
    private static Map<String, Object> singletonMap = new HashMap<>();
    
    public static void putInstance(String key, Object instance){
        if(StringUtils.isNotBlank(key) && instance != null){
            if(!singletonMap.containsKey(key)){
                singletonMap.put(key, instance);
            }
        }
    }
    
    public static Object getInstance(String key){
        return singletonMap.get(key);
    }
}
  1. 创建测试类
/**
 * @author lijiayin
 */
public class ContainerTest {

    public static void main(String[] args) {
        Thread t1 = new Thread(() -> {
            ContainerSingleton.putInstance("object", new Object());
            Object instance = ContainerSingleton.getInstance("object");
            System.out.println(Thread.currentThread().getName() + ":" + instance);
        });
        Thread t2 = new Thread(() -> {
            ContainerSingleton.putInstance("object", new Object());
            Object instance = ContainerSingleton.getInstance("object");
            System.out.println(Thread.currentThread().getName() + ":" + instance);
        });
        t1.start();
        t2.start();
        System.out.println("program end");
    }
}
  1. 测试结果


    测试结果.png
  2. 注意
    虽然返回是同一个对象,但是特殊场景存在问题。
    第一个线程放入一个对象,第二个线程也放入一个对象,然后都调用返回方法,返回的都是第二个对象。此时返回的是同一个对象。
    当第一个线程放入一个对象时,调用返回方法,返回第一个对象;然后第二个线程才放入第二个对象,调用返回方法,返回第二个对象。此时返回的是不同的对象。
    使用时要特别注意!

ThreadLocal线程“单例”

代码演示

  1. 创建ThreadLocalSingleton类
/**
 * @author lijiayin
 */
public class ThreadLocalSingleton {
    private static final ThreadLocal<ThreadLocalSingleton> threadLocalSingleton
            = ThreadLocal.withInitial(ThreadLocalSingleton::new);
    private ThreadLocalSingleton(){
        
    }
    public static ThreadLocalSingleton getInstance(){
        return threadLocalSingleton.get();
    }
}
  1. 创建测试类
/**
 * @author lijiayin
 */
public class ThreadLocalTest {

    public static void main(String[] args) {
        System.out.println("main:" + ThreadLocalSingleton.getInstance());
        System.out.println("main:" + ThreadLocalSingleton.getInstance());
        System.out.println("main:" + ThreadLocalSingleton.getInstance());
        System.out.println("main:" + ThreadLocalSingleton.getInstance());
        Thread t1 = new Thread(() -> {
            ThreadLocalSingleton instance = ThreadLocalSingleton.getInstance();
            System.out.println(Thread.currentThread().getName() + ":" + instance);
        });
        Thread t2 = new Thread(() -> {
            ThreadLocalSingleton instance = ThreadLocalSingleton.getInstance();
            System.out.println(Thread.currentThread().getName() + ":" + instance);
        });
        t1.start();
        t2.start();
        System.out.println("program end");
    }
}
  1. 测试结果


    测试结果.png
  2. 结论
    这种方式,在单个线程中是同一个对象,对象为线程私有。

开源框架应用

  1. 饿汉式:Runtime
  2. 容器单例:Desktop.getDesktop()

相关文章

  • 设计模式第二篇、单例设计模式

    目录1、什么是单例设计模式2、单例设计模式的简单实现3、单例设计模式面临的两个问题及其完整实现4、单例设计模式的应...

  • 设计模式之一:单例模式

    摘要:设计模式之一:单例模式目录介绍1.单例模式介绍2.单例模式定义3.单例模式使用场景4.单例模式的实现方式 4...

  • 单例模式Java篇

    单例设计模式- 饿汉式 单例设计模式 - 懒汉式 单例设计模式 - 懒汉式 - 多线程并发 单例设计模式 - 懒汉...

  • 单例模式

    目录 1、设计模式简介 2、什么是单例模式 3、单例模式应用场合 4、单例模式作用 5、单例模式3个要点/要素 6...

  • python中OOP的单例

    目录 单例设计模式 __new__ 方法 Python 中的单例 01. 单例设计模式 设计模式设计模式 是 前人...

  • 单例

    目标 单例设计模式 __new__ 方法 Python 中的单例 01. 单例设计模式 设计模式设计模式 是 前人...

  • 设计模式 - 单例模式

    设计模式 - 单例模式 什么是单例模式 单例模式属于创建型模式,是设计模式中比较简单的模式。在单例模式中,单一的类...

  • 设计模式

    常用的设计模式有,单例设计模式、观察者设计模式、工厂设计模式、装饰设计模式、代理设计模式,模板设计模式等等。 单例...

  • 2018-04-08php实战设计模式

    一、单例模式 单例模式是最经典的设计模式之一,到底什么是单例?单例模式适用场景是什么?单例模式如何设计?php中单...

  • python 单例

    仅用学习参考 目标 单例设计模式 __new__ 方法 Python 中的单例 01. 单例设计模式 设计模式设计...

网友评论

      本文标题:设计模式:单例模式(4)

      本文链接:https://www.haomeiwen.com/subject/hhbmlctx.html