原始版本

public static Object getInstance() {
if (instance != null) {
return instance;
} instance = new Object();
return instance;
}

不足:没有考虑多线程

版本1:

public static synchronized  Object getInstance() {
if (instance != null) {
return instance;
} instance = new Object();
return instance;
}

不足:这样会导致每次调用都是同步,效率低

double-lock-check

private static final Object lock = new Object();
private static volatile Object instance; // must be declared volatile public static Object getInstance() {
if (instance == null) { // avoid sync penalty if we can
synchronized (lock) { // declare a private static Object to use for mutex
if (instance == null) { // have to do this inside the sync
instance = new Object();
}
}
} return instance;
}

单例模式多线程安全写法(double-lock-check)的更多相关文章

  1. python的threading的使用(join方法,多线程,锁threading.Lock和threading.Condition

    一.开启多线程方法一 import threading,time def write1(): for i in range(1,5): print('1') time.sleep(1) def wri ...

  2. 重新想象 Windows 8 Store Apps (46) - 多线程之线程同步: Lock, Monitor, Interlocked, Mutex, ReaderWriterLock

    [源码下载] 重新想象 Windows 8 Store Apps (46) - 多线程之线程同步: Lock, Monitor, Interlocked, Mutex, ReaderWriterLoc ...

  3. C#单例模式的多种写法

    它的主要特点不是根据客户程序调用生成一个新的实例,而是控制某个类型的实例数量-唯一一个.(<设计模式-基于C#的工程化实现及扩展>,王翔).也就是说,单例模式就是保证在整个应用程序的生命周 ...

  4. java 单例模式5种写法

    学习整理 饱汉模式(懒汉模式) // 饱汉 // UnThreadSafe public class Singleton1 { private static Singleton1 singleton ...

  5. 多线程的并发问题,lock用法

    开启多个线程,每个线程中多次操作公共变量 using System; using System.Collections.Generic; using System.Linq; using System ...

  6. 多线程编程学习四(Lock 的使用).

    一.前言 本文要介绍使用Java5中 Lock 对象,同样也能实现同步的效果,而且在使用上更加方便.灵活,主要包括 ReentrantLock 类的使用和ReentrantReadWriteLock ...

  7. 多线程系列三:Lock和Condition

    有了synchronized为什么还要Lock? 因为Lock和synchronized比较有如下优点 1. 尝试非阻塞地获取锁 2. 获取锁的过程可以被中断 3. 超时获取锁 Lock的标准用法 p ...

  8. Objective-c单例模式的正确写法--用dispatch 线程安全

    单例模式在iOS开发中可能算是最常用的模式之一了,但是由于oc本身的语言特性,想要写一个正确的单例模式相对来说比较麻烦,这里我就抛砖引玉来聊一聊iOS中单例模式的设计思路.关于单例模式更多的介绍请参考 ...

  9. Spring单例模式多线程安全问题-有状态的Bean

    Spring单例与线程安全小结 一.Spring单例模式与线程安全 Spring框架里的bean,或者说组件,获取实例的时候都是默认的单例模式,这是在多线程开发的时候要尤其注意的地方. 单例模式的意思 ...

随机推荐

  1. Linux下部署多个Tomcat(完整)

    Linux下部署多个Tomcat 1.环境:1.1.Centos 5.01.2.apache-tomcat-6.0.18 2.需要解决一下几个问题2.1.不同的tomcat启动和关闭监听不同的端口2. ...

  2. 强大的JQuery表单验证插件 FormValidator使用介绍

    jQuery formValidator表单验证插件是客户端表单验证插件. 在做B/S开发的时候,我们经常涉及到很多表单验证,例如新用户注册,填写个人资料,录入一些常规数据等等.在这之前,页面开发者( ...

  3. 学习 rostopic

    rostopic pub可以把数据发布到当前某个正在广播的话题上. rostopic pub [topic] [msg_type] [args] 示例 $ rostopic pub - /turtle ...

  4. Struts2 框架使用 核心以及其他详细配置

    因为在使用SSH框架的过程,关于struts2的配置比较繁琐,所以做个总结. 一.导入并且关联其他XML 1.   因为在核心配置文件(Struts2.xml)中,如果存在很多需要配置的Action项 ...

  5. 口语详解|为什么“how to say”是错的?

    你有没有说过一些印象深刻的中式英语呢?为什么有的英语会被称之为中式英语想必你大概知道,但是如何把中式英语使用正确你知道吗?今天,跟着小编来看看吧.By the way,今天的主角是"how ...

  6. gateone安装使用

    下载地址 https://github.com/liftoff/GateOne unzip GateOne-master.zip cd GateOne-master/ python setup.py ...

  7. field, or, more generally, in a ring or even a semiring 数域、环、半环

    小结: 1.数域.环.半环 :一般化.泛化 https://en.wikipedia.org/wiki/Matrix_multiplication In mathematics, matrix mul ...

  8. SHOW PROCESSLIST shows which threads are running 查看线程 解决瓶颈

    小结: 1.查看全部线程: https://dev.mysql.com/doc/refman/8.0/en/show-processlist.html MySQL 8.0 Reference Manu ...

  9. xcode工程编译错误:一般错误总结

    1.Apple LLVM 8.0 Error Group /’all-product-headers.yaml’ not found 最近升级了xcode打包后出现了个BUG,记录解决的方法. 现象: ...

  10. [knowledge][bigdata] nosql

    几款主流nosq数据库对比:http://www.cnblogs.com/vajoy/p/5471308.html Redis VS MongoDB:http://www.jianshu.com/p/ ...