JAVA设计模式之单例(singleton)
一、饿汉式
/**
* 饿汉式
*/
public class Singleton01 { private static final Singleton01 instance = new Singleton01(); private Singleton01(){} public static Singleton01 getInstance(){
return instance;
} public static void main(String[] args) {
System.out.println(Singleton01.getInstance().hashCode());
System.out.println(Singleton01.getInstance().hashCode());
}
}
二、懒汉式
/**
* 懒汉式
*/
public class Singleton02 { private static Singleton02 instance; private Singleton02(){} public static Singleton02 getInstance(){
if(instance == null){
instance = new Singleton02();
}
return instance;
} public static void main(String[] args) {
System.out.println(Singleton02.getInstance().hashCode());
System.out.println(Singleton02.getInstance().hashCode());
}
}
三、懒汉式线程不安全测试
/**
* 懒汉式 线程不安全测试
*/
public class Singleton03 { private static Singleton03 instance;
private static Object obj = new Object(); private Singleton03(){} public static Singleton03 getInstance(){ if(instance == null){
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
instance = new Singleton03(); }
return instance;
} public static void main(String[] args) {
for(int i = 0; i< 100;i++) {
new Thread (() ->{
System.out.println(Singleton03.getInstance().hashCode());
}).start();
}
}
}
四、懒汉式线程安全写法(坑),并不能保证线程安全
/**
* 懒汉式 线程不安全
*/
public class Singleton03 { private static Singleton03 instance; private Singleton03(){} public static Singleton03 getInstance(){ if(instance == null){
synchronized (Singleton03.class) {
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
instance = new Singleton03();
}
}
return instance;
} public static void main(String[] args) {
for(int i = 0; i< 100;i++) {
new Thread (() ->{
System.out.println(Singleton03.getInstance().hashCode());
}).start();
}
}
}
5、懒汉式线程安全写法DCL,必须添加volatile关键字防止指令重排序
/**
* 懒汉式 线程安全
*/
public class Singleton04 { private static volatile Singleton04 instance; private Singleton04(){} public static Singleton04 getInstance(){ if(instance == null){
synchronized (Singleton04.class) {
if(instance == null){
try {
Thread.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
instance = new Singleton04();
}
}
}
return instance;
} public static void main(String[] args) {
for(int i = 0; i< 100;i++) {
new Thread (() ->{
System.out.println(Singleton04.getInstance().hashCode());
}).start();
}
}
}
6、单例模式静态内部类实现
/**
* 静态内部类实现 线程安全
*/
public class Singleton05 { private Singleton05(){} private static class SingletonHolder {
private static final Singleton05 instance = new Singleton05();
} public static Singleton05 getInstance(){
return SingletonHolder.instance;
} public static void main(String[] args) {
for(int i = 0; i< 100;i++) {
new Thread (() ->{
System.out.println(Singleton05.getInstance().hashCode());
}).start();
}
}
}
7、终极实现之枚举类,Joshua Bloch 的《effective java》中推荐写法,防止反射和反序列化并且线程安全
/**
* 枚举类实现 线程安全
*/
public enum Singleton06 { instance;
public static void main(String[] args) {
for(int i = 0; i< 100;i++) {
new Thread (() ->{
System.out.println(Singleton06.instance.hashCode());
}).start();
}
}
}
JAVA设计模式之单例(singleton)的更多相关文章
- Java设计模式之 — 单例(Singleton)
转载请注明出处:http://blog.csdn.net/guolin_blog/article/details/8860649 写软件的时候经常需要用到打印日志功能,可以帮助你调试和定位问题,项目上 ...
- Java设计模式之单例
一.Java中的单例: 特点: ① 单例类只有一个实例 ② 单例类必须自己创建自己唯一实例 ③ 单例类必须给所有其他对象提供这一实例 二.两种模式: ①懒汉式单例<线程不安全> 在类加载时 ...
- [译]Java 设计模式之单例
(文章翻译自Java Design Pattern: Singleton) 单例是在Java最经常被用到的设计模式.它通过阻止其他的实例化和修改来用于控制创建对象的数目.这一特性可应用于那些当只有一个 ...
- java设计模式_单例
public class Singleton { public static void main(String[] args) throws Exception { System.out.printl ...
- JAVA设计模式:单例设计
1.单例设计Singleton的引出 单例设计,从名字上首先可以看出单---即只有一个,例---只的是实例化对象:那么单例也就是说一个类,只产生了一个实例化对象.但是我们都知道,一个类要产生实例化对象 ...
- java设计模式之单例设计模式和多例设计模式
单例设计模式:构造方法私有化,在类的内部定义static属性和方法,利用static方法来取得本类的实例化对象:无论外部产生多少个实例化对象,本质上只有一个实例化对象 饿汉式单例设计 class Si ...
- JAVA中实现单例(Singleton)模式的八种方式
单例模式 单例模式,是一种常用的软件设计模式.在它的核心结构中只包含一个被称为单例的特殊类.通过单例模式可以保证系统中,应用该模式的类一个类只有一个实例.即一个类只有一个对象实例. 基本的实现思路 单 ...
- Java设计模式之单例设计模式总结
package singleton; /**单例设计模式 饿汉式 * * @author gx *这种方式基于classloder机制避免了多线程的同步问题,不过,instance在类装载时就实例化, ...
- java设计模式之单例设计模式
单例设计模式 保证一个类在使用过程中,只有一个实例.优势就是他的作用,这个类永远只有一个实例. 优势:这个类永远只有一个实例,占用内存少,有利于Java垃圾回收. 单例设计模式关键点 私有的构造方法. ...
随机推荐
- nmon 的下一代工具 njmon
njmon njmon = nmon + JSON format + real-time push to a stats database + instant graphing of "al ...
- GeiGebra指令
- ASE课程总结 by 林建平
设想和目标 1. 我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的辅助用户在阅读英文文献时记忆生词,提高用户的生词量,减少用户的阅读障碍.定义非常清晰,要有查 ...
- 使用jquery清空input 文本框中的内容
只需要将文本框的值置为空即可: function resetBtn(){ $("#name").val(""); }
- SQL语句加锁分析
背景 MySQL中SQL加锁的情况十分复杂,不同隔离级别.不同索引类型.索引是否命中的SQL加锁各不相同. 然而在分析死锁过程当中,熟知各种情况的SQL加锁是分析死锁的关键,因此需要将MySQL的各种 ...
- 基于thinkphp3.2.3开发的CMS内容管理系统 - ThinkPHP框架
基于thinkphp3.2.3开发的CMS内容管理系统 thinkphp版本:3.2.3 功能: --分类栏目管理 --文章管理 --用户管理 --友情链接管理 --系统设置 目前占时这些功能,更多功 ...
- 深度剖析前端JavaScript中的原型(JS的对象原型)
这张图片有点劝退了,哈哈哈~ 通过原型机制,JavaScript 中的对象从其他对象继承功能特性:这种继承机制与经典的面向对象编程语言的继承机制不同.本文将探讨这些差别,解释原型链如 ...
- Python自然语言处理实战核心技术与算法,Python自然语言处理,PyTorch深度学习实战【下载】
本人买的,无私贡献给大家,无解压密码 下载地址: 链接:https://pan.baidu.com/s/1cJtnhEQSXHVMgygr8PHh9A 提取码:a54u
- Linux中find常见用法示例 ·find path -option [ -print ] [ -exec -ok command ] {} \;
find命令的参数: pathname: find命令所查找的目录路径.例如用.来表示当前目录,用/来表示系统根目录.-print: find命令将匹配的文件输出到标准输出.-exec: find命令 ...
- PHPSTORM快捷键On Mac
Command+Shift+O快捷定位文件,需要输入文件名称 Command+鼠标左键点击方法,快捷转到方法实现 Command+Option+方向左键,快捷返回上一步跳转方法之前 Shift+F6统 ...