1    源码解析

1.1  单例解析1

1.2  单例解析2(容器单例)

1.3  单例解析3

1.4  单例解析4

1    源码解析
1.1  单例解析1

java.lang.Runtime

  1. /**
  2. * 饿汉式加载,初始化的时候,就已经new出了对象
  3. */
  4. private static Runtime currentRuntime = new Runtime();
  5.  
  6. /**
  7. * Returns the runtime object associated with the current Java application.
  8. * Most of the methods of class <code>Runtime</code> are instance
  9. * methods and must be invoked with respect to the current runtime object.
  10. *
  11. * @return the <code>Runtime</code> object associated with the current
  12. * Java application.
  13. */
  14. public static Runtime getRuntime() {
  15. return currentRuntime;
  16. }
1.2  单例解析2(容器单例)

java.awt.Desktop(cs)

  1. /**
  2. * Returns the <code>Desktop</code> instance of the current
  3. * browser context. On some platforms the Desktop API may not be
  4. * supported; use the {@link #isDesktopSupported} method to
  5. * determine if the current desktop is supported.
  6. * @return the Desktop instance of the current browser context
  7. * @throws HeadlessException if {@link
  8. * GraphicsEnvironment#isHeadless()} returns {@code true}
  9. * @throws UnsupportedOperationException if this class is not
  10. * supported on the current platform
  11. * @see #isDesktopSupported()
  12. * @see java.awt.GraphicsEnvironment#isHeadless
  13. */
  14.  
  15. /*
  16. * 同步锁,context取对象,如果该对象为为null,new出新的对象,然后放入context
  17. */
  18. public static synchronized Desktop getDesktop(){
  19. if (GraphicsEnvironment.isHeadless()) throw new HeadlessException();
  20. if (!Desktop.isDesktopSupported()) {
  21. throw new UnsupportedOperationException("Desktop API is not " +
  22. "supported on the current platform");
  23. }
  24.  
  25. sun.awt.AppContext context = sun.awt.AppContext.getAppContext();
  26. Desktop desktop = (Desktop)context.get(Desktop.class);
  27. if (desktop == null) {
  28. desktop = new Desktop();
  29. context.put(Desktop.class, desktop);
  30. }
  31. /**
  32. * context put的时候加上同步锁,可以避免多线程put异常
  33. */
  34. public Object put(Object var1, Object var2) {
  35. HashMap var3 = this.table;
  36. synchronized(this.table) {
  37. MostRecentKeyValue var4 = this.mostRecentKeyValue;
  38. if (var4 != null && var4.key == var1) {
  39. var4.value = var2;
  40. }
  41.  
  42. return this.table.put(var1, var2);
  43. }
  44. }
1.3  单例解析3(Spring框架获取单例对象)

spring中的单例是bean作用域中的一个,作用域在每个应用程序的上下文中只创建一个我们设置属性的实例,

和我们的单例的区别是:spring将实例的数量限制的作用域在整个应用程序的上下文,而java应用程序中,是将类加载器的数量限制在给定的类加载器的整个空间里。

所以说,在spring中启动多个容器的时候,每个容器即使是单例的,都可以拿到这个对象。

  1. public final T getObject() throws Exception {
  2. if (this.isSingleton()) {
  3. return this.initialized ? this.singletonInstance : this.getEarlySingletonInstance();
  4. } else {
  5. return this.createInstance();
  6. }
  7. }
  8.  
  9. /*
  10. * 如果被初始化,获取早期的单例对象
  11. *
  12. */
  13. //通过代理去拿新对象
  14. private T getEarlySingletonInstance() throws Exception {
  15. Class<?>[] ifcs = this.getEarlySingletonInterfaces();
  16. if (ifcs == null) {
  17. throw new FactoryBeanNotInitializedException(this.getClass().getName() + " does not support circular references");
  18. } else {
  19. if (this.earlySingletonInstance == null) {
  20. this.earlySingletonInstance = Proxy.newProxyInstance(this.beanClassLoader, ifcs, new AbstractFactoryBean.EarlySingletonInvocationHandler());
  21. }
  22.  
  23. return this.earlySingletonInstance;
  24. }
  25. }
1.4  单例解析4(基于threadLocal的线程案例)(mybaties获取单例对象)

mybaties上下文保证了每个线程各自的数据,每个线程自己的上下文,自己保存好

  1. private static final ThreadLocal<ErrorContext> LOCAL = new ThreadLocal<ErrorContext>();
  2.  
  3. private ErrorContext() {
  4. }
  5.  
  6. public static ErrorContext instance() {
  7. ErrorContext context = LOCAL.get();
  8. if (context == null) {
  9. context = new ErrorContext();
  10. LOCAL.set(context);
  11. }
  12. return context;
  13. }

设计模式课程 设计模式精讲 8-11 单例模式源码解析(jdk+spring+mybaties)的更多相关文章

  1. 设计模式课程 设计模式精讲 7-3 建造者模式源码解析(jdk+guava+spring+mybaties)

    1 源码解析 1.1 jdk解析 1.2 guava解析 1.3 spring解析 1.4 mybaties解析 1 源码解析 1.1 jdk解析 String public StringBuilde ...

  2. 设计模式(十)——组合模式(HashMap源码解析)

    1 看一个学校院系展示需求 编写程序展示一个学校院系结构:需求是这样,要在一个页面中展示出学校的院系组成,一个学校有多个学院, 一个学院有多个系.如图: 2 传统方案解决学校院系展示 3 传统方案解决 ...

  3. 设计模式-简单工厂Coding+jdk源码解析

    感谢慕课geely老师的设计模式课程,本套设计模式的所有内容均以课程为参考. 前面的软件设计七大原则,目前只有理论这块,因为最近参与项目重构,暂时没有时间把Coding的代码按照设计思路一点点写出来. ...

  4. Okhttp3源码解析(4)-拦截器与设计模式

    ### 前言 回顾: [Okhttp的基本用法](https://www.jianshu.com/p/8e404d9c160f) [Okhttp3源码解析(1)-OkHttpClient分析](htt ...

  5. 6 admin(注册设计)源码解析、单例模式

    1.单例模式 https://www.cnblogs.com/yuanchenqi/articles/8323452.html 单例模式(Singleton Pattern)是一种常用的软件设计模式, ...

  6. 给jdk写注释系列之jdk1.6容器(11)-Queue之ArrayDeque源码解析

    前面讲了Stack是一种先进后出的数据结构:栈,那么对应的Queue是一种先进先出(First In First Out)的数据结构:队列.      对比一下Stack,Queue是一种先进先出的容 ...

  7. Java 集合系列11之 Hashtable详细介绍(源码解析)和使用示例

    概要 前一章,我们学习了HashMap.这一章,我们对Hashtable进行学习.我们先对Hashtable有个整体认识,然后再学习它的源码,最后再通过实例来学会使用Hashtable.第1部分 Ha ...

  8. Scala 深入浅出实战经典 第65讲:Scala中隐式转换内幕揭秘、最佳实践及其在Spark中的应用源码解析

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 ...

  9. Scala 深入浅出实战经典 第61讲:Scala中隐式参数与隐式转换的联合使用实战详解及其在Spark中的应用源码解析

    王家林亲授<DT大数据梦工厂>大数据实战视频 Scala 深入浅出实战经典(1-87讲)完整视频.PPT.代码下载: 百度云盘:http://pan.baidu.com/s/1c0noOt ...

随机推荐

  1. 使用jps查看JVM进程信息

    VM进程状态工具 - 列出目标系统上已检测的HotSpot Java虚拟机进程信息.可直接在装有java运行环境的Windows 或者 Linux机器上使用命令行执行jps命令.一个典型的应用场景,例 ...

  2. [Bug合集] java.lang.IllegalStateException: Could not find method onClickcrea(View) in a parent

    出现场景: 在一个Button中定义了onclick属性,值为startChat. 在Activity中定义一个方法. public void startChat(View view){} 运行时,点 ...

  3. ES-windows版本设置远程访问

    1,官网下载 2,下载完解压 3,修改配置文件 elasticsearch.yml network.host: 0.0.0.0http.port: 9200transport.host: localh ...

  4. Mybatis 的分页插件 PageHelper

    我用的版本是PageHelper-4.1.1.Mybatis-3.3.0 PageHelper 依赖于 jsqlparser-0.9.4.jar   使用方法: 1.根据Mybatis的版本下载对应版 ...

  5. Spring Boot Shiro 使用教程

    Apache Shiro 已经大名鼎鼎,搞 Java 的没有不知道的,这类似于 .Net 中的身份验证 form 认证.跟 .net core 中的认证授权策略基本是一样的.当然都不知道也没有关系,因 ...

  6. :after/::after和:before/::before的异同

    相同点 都可以用来表示伪类对象,用来设置对象前的内容:before和::before写法是等效的; :after和::after写法是等效的不同点 :before/:after是Css2的写法,::b ...

  7. 案例:WLC HA主WLC进入维护模式

    案例场景: 如图所示,7609-1和7609-2分别是网络中的核心设备,起了HSRP,7609-1连接的是WLC-1,,7609-2连接的是WLC-2,WLC1和WLC2的RP口相互连接. WLC的管 ...

  8. 重新梳理IT知识之java-01语法(一)

    标识符的命名规范 包名:xxxyyyzzz 类名.接口名:XxxYyyZzz (大驼峰) 变量名.方法名:xxxYyyZzz 常量名:XXX_YYY_ZZZ //**************强制类型转 ...

  9. ypACM社团年终赛暨实验室选拔赛题解

    记得补题,题目两小时半还是挺困难ak的,毕竟我验题也验了几天的时间,题目基本没有锅.题目基本属于简单题 我的三道题都是很基本的题目,希望大家补题 这些题解都是我写的,如果有疑问可以qq问我 所有的核心 ...

  10. java编译中出现了Exception in thread “main" java.lang.UnsupportedClassVersionError

    这个问题确实是由较高版本的JDK编译的java class文件试图在较低版本的JVM上运行产生的错误. 1.解决措施就是保证jvm(java命令)和jdk(javac命令)版本一致.如果是linux版 ...