Runtime源码分析 - 单例设计模式
发布日期:2021-06-30 16:57:27
浏览次数:2
分类:技术文章
本文共 2508 字,大约阅读时间需要 8 分钟。
Runtime源码
public class Runtime { private static Runtime currentRuntime = new Runtime(); /** * Returns the runtime object associated with the current Java application. * Most of the methods of classRuntime
are instance * methods and must be invoked with respect to the current runtime object. * * @return theRuntime
object associated with the current * Java application. */ public static Runtime getRuntime() { return currentRuntime; } /** Don't let anyone else instantiate this class */ private Runtime() { }}
g o o g l e \color{#4285f4}{g}\color{#ea4335}{o}\color{#fbbc05}{o}\color{#4285f4}{g}\color{#34a853}{l}\color{#ea4335}{e} google
分析
- 先看构造:
/** Don't let anyone else instantiate this class */ private Runtime() { }
构造设为private,无法调用new对象。
- 在看如何获取对象:
用 public static 的方法 g e t R u n t i m e ( ) \color{#ea4335}{getRuntime()} getRuntime(),可以通过静态调用获得对象。
/** * Returns the runtime object associated with the current Java application. * Most of the methods of classRuntime
are instance * methods and must be invoked with respect to the current runtime object. * * @return theRuntime
object associated with the current * Java application. */ public static Runtime getRuntime() { return currentRuntime; }
- 最后,看单例的来源:
private static Runtime currentRuntime = new Runtime();
在类初始化时候new一次对象,用 c u r r e n t R u n t i m e \color{#4285f4}{currentRuntime} currentRuntime引用。
确保了,对象唯一,即“单例” 其中: private 确保 c u r r e n t R u n t i m e \color{#4285f4}{currentRuntime} currentRuntime不被随意修改。 static 确保能被 g e t R u n t i m e ( ) \color{#ea4335}{getRuntime()} getRuntime()方法调用饿汉式 or 懒汉式
上述方法为“饿汉式”,即在类初始化时候就 new 出唯一的对象。
还有一种“懒汉式”,即在调用 g e t R u n t i m e ( ) \color{#ea4335}{getRuntime()} getRuntime()方法的时候才 new 出唯一的对象。 只需要稍微改下上面代码即可变为“懒汉式”。public class Runtime { private static Runtime currentRuntime = null; /** * Returns the runtime object associated with the current Java application. * Most of the methods of classRuntime
are instance * methods and must be invoked with respect to the current runtime object. * * @return theRuntime
object associated with the current * Java application. */ public static Runtime getRuntime() { if(currentRuntime == null){ currentRuntime = new Runtime(); } return currentRuntime; } /** Don't let anyone else instantiate this class */ private Runtime() { }}
转载地址:https://lawsssscat.blog.csdn.net/article/details/103012982 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!
发表评论
最新留言
关注你微信了!
[***.104.42.241]2024年04月22日 15时57分49秒
关于作者
喝酒易醉,品茶养心,人生如梦,品茶悟道,何以解忧?唯有杜康!
-- 愿君每日到此一游!
推荐文章
《知识图谱》阅读笔记(七)
2019-04-30
《知识图谱》阅读笔记(九)
2019-04-30
【超越白皮书7】你需要知道关于ETH2.0的几个事实
2019-04-30
超越白皮书8:穿云而过的闪电网络
2019-04-30
AMM做市无常损失对冲分析系列(一)—— 损益及期权对冲模型构建
2019-04-30
JS中document对象和window对象有什么区别
2019-04-30
【python练习题】遍历1
2019-04-30
【matlab】显示图片且下方显示文字
2019-04-30
关于greater<int>以及类模板的一些理解
2019-04-30
对于时间复杂度的通俗理解
2019-04-30
如何输入多组数据并输出每组数据的和?
2019-04-30
基于CentOS 7的Linux常用命令行命令
2019-04-30
行阶梯型矩阵
2019-04-30
信号量机制
2019-04-30
临界资源与临界区
2019-04-30
matlab中uint8,double,im2double和im2uint8的区别
2019-04-30
数字图像处理总复习
2019-04-30
图像去噪(包含修正的阿尔法均值滤波及通用滤波方法代码)
2019-04-30
SVM进行人脸检测
2019-04-30
C++学习笔记
2019-04-30