卓二妹原创,转载请注明来源。
@ThreadSafe public class SafePoint { @GuardedBy("this") private int x, y; private SafePoint(int[] a) { this(a[0], a[1]); } public SafePoint(SafePoint p) { this(p.get()); } public SafePoint(int x, int y) { this.x = x; this.y = y; } public synchronized int[] get() { return new int[] { x, y }; } public synchronized void set(int x, int y) { this.x = x; this.y = y; } }
java并发编程中对这段代码有一段让人比较费解的描述:
写道
The private constructor exists to avoid the race condition that would occur if the copy constructor were implemented as this(p.x, p.y); this is an example of the private constructor capture idiom (Bloch and Gafter, 2005).
这里如果将private SafePoint(int[] a) { this(a[0], a[1]); }方法改为public就会有race condition问题。为什么呢?
因为如果我们要复制一个SafePoint对象时,我们会首先想到编写下面这样一个方法:
public SafePoint(SafePoint safePoint){ this(safePoint.x, safePoint.y); }
但是当我们在通过复制originalSafePoint创建新的SafePoint对象时,可能有一个线程正在调用originalSafePoint.setXY方法,导致读取safePoint.x, safePoint.y这对值可能一个更新了而一个没更新,出现线程不安全。因此x,y必须同时读取出来。所以在复制的时候,不能像上面那样调用,而要用getXY()把x y一起取出来。
public SafePoint(SafePoint safePoint){ int [] xy = safePoint.getXY(); this(xy[0], xy[1]); }
上面的代码会有编译错误,因为this()必须是body的第一行:
那么好吧,那我们这样写就行了是吗?
public SafePoint(SafePoint originalSafePoint){ this(originalSafePoint.getXY()[0],originalSafePoint.getXY()[1]); }
显然这样也是有问题的,因为originalSafePoint.getXY()被调用了两次,这时候两次取出来的x y已经不是一对值了。
因此我们只有提供一个构造方法接受getXY()返回的数组作为参数,来复制一个对象:
public SafePoint(SafePoint originalSafePoint){ this(originalSafePoint.getXY()); } private SafePoint(int [] xy){ this(xy[0], xy[1]); }
这样就避免了上面的copy constructor的问题。现在再来读一下作者说的这句话就清楚很多了:
写道
The private constructor exists to avoid the race condition that would occur if the copy constructor were implemented as this(p.x, p.y);
私有的构造方法的存在,是为了避免在复制构造函数(即SafePoint(SafePoint originalSafePoint))的中因调用this(p.x, p.y)而引起的race condition。
相关推荐
Concurrency is always a challenge for developers and writing concurrent programs can be extremely hard. There is a number of things that could potentially blow up and the complexity of systems rises ...
java 并发编程非常丰富的资料整理汇总,很好的参考 。
不需要分的Java-Concurrency-in-Practice.rar
CPP-Concurrency-In-Action-2ed-2019-master Makeddown格式
Java Concurrency Patterns and Features Concurrency Patterns and features found in Java, through multithreaded programming. Features: Threads and Runnables Locks Intrinsic Explicit Reentrant Read...
单独地记录HTTP requests/responses,可视化它们的并发并在一个请求上下文中报告日记/错误
the-art-of-java-concurrency-programming:Java并发编程的艺术原始代码
java_concurrency_in_practice_source java多线程 java多线程 java多线程
java-concurrency-in-practice-exercises 看书时的一些练习
资源分类:Python库 所属语言:Python 资源全名:oslo.concurrency-4.0.2-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
Java Concurrency in Practice 英文无水印pdf pdf所有页面使用FoxitReader和PDF-XChangeViewer测试都可以打开 本资源转载自网络,如有侵权,请联系上传者或csdn删除 本资源转载自网络,如有侵权,请联系上传者...
Learning-Concurrency-in-Python-Build-highly-efficient-robust-and-concurrent-applications.pdf
Concurrency-in-Go_Tools-and-Techniques-for-Developers
Delphi.High.Performance.Applications.Concurrency--中文版.pdf
Using the concurrency building blocks in java.util.concurrent Performance optimization dos and don'ts Testing concurrent programs Advanced topics such as atomic variables, nonblocking algorithms, ...
资源分类:Python库 所属语言:Python 资源全名:django-concurrency-0.4.tar.gz 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
开源项目-kevchn-go-concurrency-patterns.zip,Implementations of Golang Concurrency Patterns from Rob Pike's 2012 Talk
资源分类:Python库 所属语言:Python 资源全名:oslo.concurrency-3.15.0-py2.py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
Java 9 Concurrency Cookbook - Second Edition by Javier Fernandez Gonzalez English | 25 Apr. 2017 | ASIN: B01KOG6V5M | 594 Pages | AZW3 | 4.11 MB Key Features Get detailed coverage of important ...
南开大学数据库原理课件lecture9-Concurrency-Control.ppt