- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章9种Java单例模式详解(推荐)由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
单例模式的特点 。
懒汉式(线程不安全) 。
其主要表现在单例类在外部需要创建实例化对象时再进行实例化,进而达到lazy loading 的效果.
通过静态方法 getsingleton() 和private 权限构造方法为创建一个实例化对象提供唯一的途径.
不足:未考虑到多线程的情况下可能会存在多个访问者同时访问,发生构造出多个对象的问题,所以在多线程下不可用这种方法.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
/**
* @author mrroot
* @since 2018-12-17
* 懒汉式(线程不安全)
*/
public
class
singleton {
private
static
singleton singleton;
private
singleton(){
}
public
static
singleton singleton(){
if
(singleton ==
null
){
singleton =
new
singleton();
}
return
singleton;
}
}
|
懒汉式(线程安全,同步方法,不推荐使用) 。
针对懒汉式的线程不安全,自然会想到给 getsingleton() 进行 synchronized 加锁来保证线程同步.
不足:效率低。大多数情况下这个锁占用的额外资源都浪费了,每个线程在想获得类的实例时候,执行 getsingleton() 方法都要进行同步.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
/**
* @author mrroot
* @since 2019-3-27
* 懒汉式(线程安全,同步方法,不推荐使用)
*/
public
class
singleton {
private
static
singleton singleton;
private
singleton(){
}
public
static
synchronized
singleton singleton(){
if
(singleton ==
null
){
singleton =
new
singleton();
}
return
singleton;
}
}
|
饿汉式(线程安全) 。
在进行类加载时完成实例化对象的过程就是饿汉式的形式.
避免了线程同步问题,在运行这个类的时候进行加载,之后直接访问 。
不足:相比接下来的静态内部类而言,这种方法比静态内部类多了内存常驻,容易造成内存浪费,也未达到延迟加载的效果.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/**
* @author mrroot
* @since 2019-3-27
* 饿汉式(线程安全)
*/
public
class
singleton{
private
static
singleton singleton =
new
singleton();
private
singleton(){
}
public
static
singleton singleton(){
return
singleton;
}
}
|
静态内部类加载(线程安全) 。
静态内部类不会在单例加载时加载,当调用 getsingleton() 方法时才会进行加载,达到类似懒汉式效果,并且也是线程安全的.
类的静态属性只会在第一次加载类时进行初始化,所以上面的方法jvm 帮助我们保证了线程的安全性,在类进行初始化时,其他线程无法进入.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
/**
* @author mrroot
* @since 2019-3-27
* 静态内部类加载(线程安全)
*/
public
class
singleton{
private
static
singleton singleton;
private
static
class
singletoninner{
private
static
final
singleton instance =
new
singleton();
}
public
static
singleton getsingleton(){
return
singletoninner.instance;
}
}
|
枚举(线程安全) 。
自由串行化;保证只有一个实例;线程安全.
effective java 作者所提倡的方法,近乎完美,在继承场景下不适用.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
/**
* @author mrroot
* @since 2019-3-27
* 枚举(线程安全)
*/
enum
singleton{
instance;
public
void
method(){
}
}
class
test{
public
static
void
main(string[] args) {
singleton.instance.method();
}
}
|
懒汉式双重校验锁法(通常线程安全,不可保证完全安全) 。
使用同步代码块避免了第二种方法的效率低的问题,但此方法并不能完全起到线程同步的作用,与上面第一种方法产生的问题相似,多线程访问时可能产生多个对象.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
/**
* @author mrroot
* @since 2019-3-27
* 懒汉式双重校验锁法(通常线程安全,不可保证完全安全)
*/
class
singleton{
private
static
singleton singleton;
private
singleton(){
}
public
static
singleton singleton(){
if
(singleton ==
null
){
synchronized
(singleton.
class
){
if
(singleton ==
null
){
singleton =
new
singleton();
}
}
}
return
singleton;
}
}
|
懒汉式双重检查终极版 。
与第六种方法不同的是,此方法给singleton 的声明上加了关键字 volatile ,进而解决了低概率的线程不安全问题.
volatile 起到禁止指令重排的作用,在它赋值完成之前,就不会调用读操作(singleton == null).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
/**
* @author mrroot
* @since 2019-3-27
* 懒汉式双重检查终极版(volatile)
*/
class
singleton{
private
static
volatile
singleton singleton;
private
singleton(){
}
public
static
singleton singleton(){
if
(singleton ==
null
){
synchronized
(singleton.
class
){
if
(singleton ==
null
){
singleton =
new
singleton();
}
}
}
return
singleton;
}
}
|
使用 threadlocal 实现(线程安全) 。
threadlocal 会为每一个线程提供一个独立的变量副本,从而隔离了多个线程对数据的访问冲突.
对于多线程资源共享的问题,同步机制采用了“以时间换空间”的方式,而threadlocal 采用了“以空间换时间”的方式。前者仅提供一份变量,让不同的线程排队访问,而后者为每一个线程都提供了一份变量,因此可以同时访问而互不影响.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
/**
* @author mrroot
* @since 2019-3-27
* 使用 threadlocal 实现(线程安全)
*/
class
singleton{
private
static
final
threadlocal<singleton> singleton =
new
threadlocal<singleton>(){
@override
protected
singleton initialvalue(){
return
new
singleton();
}
};
private
singleton(){
}
public
static
singleton getsingleton(){
return
singleton.get();
}
}
|
使用cas 锁实现(线程安全) 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
|
/**
* @author mrroot
* @since 2019-3-27
* 使用 cas 实现(线程安全)
*/
public
class
singleton {
private
static
final
atomicreference<singleton> instance =
new
atomicreference<singleton>();
private
singleton(){
}
public
static
final
singleton getsingleton(){
for
(;;){
singleton current = instance.get();
if
(current !=
null
){
return
current;
}
current =
new
singleton();
if
(instance.compareandset(
null
,current)){
return
current;
}
}
}
public
static
void
main(string[] args) {
singleton singleton1 = singleton.getsingleton();
singleton singleton2 = singleton.getsingleton();
system.out.println(singleton1 == singleton2);
}
}
|
以上所述是小编给大家介绍的9种java单例模式详解整合,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对我网站的支持! 。
原文链接:https://www.cnblogs.com/mrroot/p/10606356.html 。
最后此篇关于9种Java单例模式详解(推荐)的文章就讲到这里了,如果你想了解更多关于9种Java单例模式详解(推荐)的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
嗨, 我是 Spark 的新手,我正在尝试使用 ML 推荐。 我的代码 df = sqlContext.createDataFrame( [(0, 0, 4.0), (0, 1, 2.0), (1,
按照目前的情况,这个问题不适合我们的问答形式。我们希望答案得到事实、引用或专业知识的支持,但这个问题可能会引发辩论、争论、投票或扩展讨论。如果您觉得这个问题可以改进并可能重新打开,visit the
平台工程是为软件开发人员创建高效生态系统的过程,帮助他们自主执行软件开发生命周期的端到端操作。平台工程旨在减少开发人员的整体认知负荷并消除流程中的瓶颈,让开发团队的体验更佳。平台工程工具通过改善开发
最近在ubuntu系统中使用自带的firefox浏览器,发现有写问题,比如登陆后,书签,历史记录等,原本在windows下同步的数据无法同步,添加书签的功能也无法使用。 经过查询资料后得知,unb
Remax是蚂蚁开源的一个用React来开发小程序的框架,采用运行时无语法限制的方案。整体研究下来主要分为三大部分:运行时原理、模板渲染原理、编译流程;看了下现有大部分文章主要集中在Reamx的运行
实验室拟态存储的项目需要通过lvs-nat模式通过lvs服务器来区隔内外网的服务,所以安全防护的重心则落在了lvs服务器之上。笔者最终选择通过firewalld放行端口的方式来实现需求,由于fire
如今,随着我们身体各类数据的指数级增长,人们需要接受的信息量越来越大,系统必须处理的难度也是越来越高。而这些正是我们需要通过交互式图表和仪表盘,来实现数据可视化的根本原因。在大幅节省用户的时间和精力
vsftpd 是“very secure FTP daemon”的缩写,安全性是它的一个最大的特点。 vsftpd 是一个 UNIX 类操作系统上运行的服务器的名字,它可以运行在诸如 Linux、
1、实现memcpy 将src所指向的内容拷贝到dst所指向的位置,拷贝len个字节。 memcpy是内存拷贝函数 memcpy在使用的时候不用考虑类型,以字节为单位进行拷贝
现在有3台服务器 s1(主),s2(从), s3(从)需要实现文件实时同步,我们可以安装Nfs服务端和客户端来实现! 1、安装 NFS 服务器所需的软件包:
本文基于Free Code Camp基本算法脚本“查找字符串中最长的单词”。 在此算法中,我们要查看每个单词并计算每个单词中有多少个字母。然后,比较计数以确定哪个单词的字符最多,并返回最长单词的长
I/O简介 I/O是Input/output的缩写,在java中,对于数据的输入和输出以流的方式进行。java.io包下提供了各种“流”类和接口,用以获取不同种类的数据,并通过标准的方法输入或输出
目录 docker容器源码部署httpd,用存储卷部署网站 创建一个httpd镜像 部署nfs 挂载 创建容器并映射
python代码如下: import webbrowser as wbimport foliumif __name__ == '__main__': loc = [30.679943, 104.0
近日,微软在 Github 上开源了一个 Python 静态类型检查工具:pyright ,引起了社区内的多方关注。 微软在开源项目上的参与力度是越来越大了,不说收购 Github 这种大的战略野
在编写多线程代码时,经常面临线程安全退出的问题。 一般情况下,选择检查标志位的方式: 在线程的while循环中,执行完例程后,都对标志位进行检查,如果标志位指示继续执行则再次执行例程,如果标志
前言 在程序中我们经常可以看到有很多的加密算法,比如说MD5 sha1等,今天我们就来了解下这下加密算法的吧,在了解之前我们需要知道一个模块嘛就是hashlib,他就是目前Python一个提供字符
java 泛型(generics)是 jdk 5 中引入的一个新特性, 泛型提供了编译时类型安全检测机制,该机制允许程序员在编译时检测到非法的类型。 泛型的本质是参数化类型,也就是说所操作的数据类型
在Python中,当我们有两个字典需要合并的时候,可以使用字典的 update 方法,例如: a = {'a': 1, 'b': 2} b = {'x': 3, 'y': 4}
有的时候我们在获取到目标电脑时候如果对方电脑又python 编译环境时可以利用python 反弹shell 主要用到python os库和sokect库 这里的服务端在目标机上运行
我是一名优秀的程序员,十分优秀!