双重锁定在单身人士

编程入门 行业动态 更新时间:2024-10-24 15:27:17
本文介绍了双重锁定在单身人士的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 这里是我自定义类的单例模式。在这段代码中,我使用双重检查锁定如下。当我在某些来源上阅读了很多帖子时,他们说双重检查是有用的,因为它可以防止两个并发线程在同一时间运行时产生两个不同的对象。

code> public class DoubleCheckLocking { public static class SearchBox { private static volatile SearchBox searchBox; //此类的私有属性 private String searchWord =; private String [] list = new String [] {Stack,Overflow}; // private constructor private SearchBox(){} //静态方法获取实例 public static SearchBox getInstance(){ if(searchBox == null){//第一次锁 synchronized(SearchBox.class){ if(searchBox == null){//第二次锁 searchBox = new搜索盒(); } } } return searchBox; } }

我仍然不太了解上面的代码。如果两个线程在实例为null时一起运行同一行代码,那么是什么问题?

if(searchBox == null) { synchronized(SearchBox.class){ if(searchBox == null){ searchBox = new SearchBox(); } } }

出现时。两个线程都会看到object为null。然后两者同步。然后,他们再次检查,仍然看到它为空。并创建两个不同的对象。 OOOPS。

请为我解释。我理解错了什么?

感谢:)

解决方案

不,因为你正在获得锁在 SearchBox.class 中,一次只能有一个线程进入同步块。所以第一个线程进入,然后找到 searchBox 并创建它,然后离开同步的块,然后第二个线程进入块,然后它发现 searchBox 不是空的,因为第一个线程已经创建了它,所以它不会创建一个新的实例 searchBox

双重检查模式用于避免每次执行代码时获取锁定,如果调用不在一起发生,那么第一个条件将失败,代码执行将不会执行锁定,从而节省资源。 p>

here is my custom class for singleton pattern. in this code, I use double-checked locking as below. As I read many posts on some source, they say that double check is useful because it prevents two concurrent threads run at same times make two different objects.

public class DoubleCheckLocking { public static class SearchBox { private static volatile SearchBox searchBox; // private attribute of this class private String searchWord = ""; private String[] list = new String[]{"Stack", "Overflow"}; // private constructor private SearchBox() {} // static method to get instance public static SearchBox getInstance() { if (searchBox == null) { // first time lock synchronized (SearchBox.class) { if (searchBox == null) { // second time lock searchBox = new SearchBox(); } } } return searchBox; } }

I still don't understand above code so much. What is the problem, if two threads together run same line of code when instance is null ?

if (searchBox == null) { synchronized (SearchBox.class) { if (searchBox == null) { searchBox = new SearchBox(); } } }

When that appear. both two threads will see object is null. then both synchronize. and then, they check again, and still see it null. and create two different objects. OOOPS.

Please explain for me. What have I understand wrong ?

Thanks :)

解决方案

No, since you are obtaining lock on the SearchBox.class, only one thread will enter the synchronized block at a time. So the first thread enters then finds searchBox and creates it and then leaves the synchronized block, then the second thread enter the block then it finds that the searchBox is not null because the first thread already created it so it will not create a new instance of searchBox

The Double checked pattern is used to avoid obtaining the lock everytime the code is executed, if the call are not happening together then the first condition will fail and the code execution will not execute the locking thus saving resources.

更多推荐

双重锁定在单身人士

本文发布于:2023-06-04 19:08:01,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/503341.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:单身   人士

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!