Comments (3)
对啊,所以才需要双重检查
from interviewguide.
对啊,所以才需要双重检查
‘ 双重检查存在的一样在于可能会有多个线程进入第一个判断,然后竞争同步锁,线程A得到了同步锁,创建了一个Singleton实例,赋值给instance,然后释放同步锁,此时线程B获得同步锁,又会创建一个Singleton实例,造成初始化覆盖。 ’
这句注释不对。
from interviewguide.
我的注释:双重检查存在的一样在于可能会有多个线程进入第一个判断,然后竞争同步锁,线程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)
- 请问频道列表是哪个接口? HOT 1
- java知识点 HOT 1
- 看不到hashMap的图 HOT 1
- github.io站点访问不了啦 HOT 2
- 关于图片的显示 HOT 2
- Linux内核学习资料
- mysql篇中更新时普通索引需要判断唯一性应该是唯一索引吧
- Redis过期key是怎么样清理的?中LRU算法 PS部分表述问题
- 楼主,我可以参与你这个开源项目或者我down一份自己按照自己的需求扩充一份吗? HOT 1
- 问题 HOT 2
- 没有按需加载 HOT 1
- 无法下载 HOT 1
- 选择菜单没有效果 HOT 3
- 不错 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from interviewguide.