博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java单例的四个小案例
阅读量:3952 次
发布时间:2019-05-24

本文共 1239 字,大约阅读时间需要 4 分钟。

java单例的四个小案例

1

package com.example.youruan.day01.entity;public class Singleton1 {
private Singleton1() {
} private static final Singleton1 singletonInstance = new Singleton1(); public static Singleton1 getInstance() {
return singletonInstance; }}

2

package com.example.youruan.day01.entity;public class Singleton2 {
private static Singleton2 instance = null; public static synchronized Singleton2 getInstance() {
if (instance == null) instance = new Singleton2(); return instance; }}

3

package com.example.youruan.day01.entity;public class Singleton3 {
private static volatile Singleton3 instance = null; public static Singleton3 getInstance() {
if (instance == null) {
synchronized (Singleton3.class) {
if (instance == null) {
instance = new Singleton3(); } } } return instance; }}

4

package com.example.youruan.day01.entity;public class Singleton4 {
private static class Holder {
static final Singleton4 instance = new Singleton4(); } public static Singleton4 getInstance() {
return Holder.instance; }}

转载地址:http://nhrwi.baihongyu.com/

你可能感兴趣的文章
“需求为王”才是根本
查看>>
高效率的危害
查看>>
寻找边缘性创新
查看>>
让创意瞄准市场
查看>>
高效经理人应具有的八个重要习惯
查看>>
优秀的领导者能读懂人才
查看>>
大智若愚也是领导力
查看>>
android如何编译MTK的模拟器
查看>>
android如何添加AP中要使用的第三方JAR文件
查看>>
利用sudo命令为Ubuntu分配管理权限
查看>>
Ubuntu下几个重要apt-get命令用法与加速UBUNTU
查看>>
Ubuntu中网页各种插件安装命令
查看>>
使用tar命令备份Ubuntu系统
查看>>
ubuntu flash 文字乱码解决方案
查看>>
在ubuntu中运行exe文件
查看>>
ubuntu安装命令
查看>>
和上司沟通必备8个黄金句
查看>>
联系查看两张卡的未接电话记录
查看>>
Python 3 之多线程研究
查看>>
APP第三方登录实现步骤
查看>>