Giter VIP home page Giter VIP logo

Comments (3)

ziping95 avatar ziping95 commented on May 10, 2024

对啊,所以才需要双重检查

from interviewguide.

dooonabe avatar dooonabe commented on May 10, 2024

对啊,所以才需要双重检查

‘ 双重检查存在的一样在于可能会有多个线程进入第一个判断,然后竞争同步锁,线程A得到了同步锁,创建了一个Singleton实例,赋值给instance,然后释放同步锁,此时线程B获得同步锁,又会创建一个Singleton实例,造成初始化覆盖。 ’
这句注释不对。

from interviewguide.

NotFound9 avatar NotFound9 commented on May 10, 2024

我的注释:双重检查存在的一样在于可能会有多个线程进入第一个判断,然后竞争同步锁,线程A得到了同步锁,创建了一个Singleton实例,赋值给instance,然后释放同步锁,此时线程B获得同步锁,又会创建一个Singleton实例,造成初始化覆盖。

你的回复:被synchronized修饰的语句块有Happens-Before规则,线程B在线程A之后进入同步块一定可以看到instance!=null

我觉得注释是没有错的,你说的线程B可以看到instance!=null不代表就线程B不会再执行

instance = new Singleton();

方法了,所以为了避免线程B再次执行一次初始化,所以synchronized修饰的语句块再加了一个if判断,你觉得有问题我们可以继续探讨,项目主页有技术群的二维码和我的微信

from interviewguide.

Related Issues (15)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.