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垃圾回收. 单例设计模式关键点 私有的构造方法. ...
随机推荐
- 谁说.NET不适合搞大数据,机器学习、人工智能
SciSharp Stack SciSharp STACK: https://scisharp.github.io/SciSharp/ 基于.NET的开源生态系统,用于数据科学.机器学习和AI. Sc ...
- ClassLoader类加载器浅见
类加载器 类加载器,它拿到.class文件,它会把他拆成两部分,将static数据转换成方法区的数据结构,然后把他放在了方法区之中. 然后在堆里面建一个类对象(Class,它可以用来实例化对象),然后 ...
- sql server临时删除/禁用非聚集索引并重新创建加回/启用的简便编程方法研究对比
前言: 由于新型冠状病毒影响,博主(zhang502219048)在2020年1月份从广东广州工作地回到广东揭阳产业转移工业园磐东街道(镇里有阳美亚洲玉都.五金之乡,素以“金玉”闻名)老家后,还没过去 ...
- A Bug's Life POJ - 2492 (种类或带权并查集)
这个题目的写法有很多,用二分图染色也可以写,思路很好想,这里我们用关于并查集的两种写法来做. 题目大意:输入x,y表示x和y交配,然后判断是否有同性恋. 1 带权并查集: 我们可以用边的权值来表示一种 ...
- I - Red and Black DFS
There is a rectangular room, covered with square tiles. Each tile is colored either red or black. A ...
- D. AB-string
https://codeforces.com/contest/1238/problem/D 题目大意:统计good string的个数,good string的定义,给定的字符串中含有回文段落, 题解 ...
- [php] phpStudy+XDebug配置
一.配置前说明: 1.phpStudy集成了XDebug扩展,所以不用单独下载XDebug. 2.打开XDebug扩展:其它选项菜单 > PHP扩展 > Xdebug 二.配置步骤: ph ...
- shiro:集成Spring(四)
基于[加密及密码比对器(三)]项目改造 引入相关依赖环境 shiro-spring已经包含 shiro-core和shiro-web 所以这两个依赖可以删掉 <!--shiro继承spring依 ...
- 用 Python 获取百度搜索结果链接
前言 近期有许多项目需要这个功能,由于Python实现起来比较简单就这么做了,代码贴下来觉得好点个赞吧~ 代码 # coding: utf-8 import os import time import ...
- pysparnn 模块使用,相似句子召回
import pysparnn.cluster_index as ci from sklearn.feature_extraction.text import TfidfVectorizer data ...