- mongodb - 在 MongoDB mapreduce 中,如何展平值对象?
- javascript - 对象传播与 Object.assign
- html - 输入类型 ="submit"Vs 按钮标签它们可以互换吗?
- sql - 使用 MongoDB 而不是 MS SQL Server 的优缺点
我正在阅读Java Concurrency in Practice
session 4.3.5一书
@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;
}
}
我不清楚它在哪里写的
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).
我知道它提供了一个 getter 来一次在数组中检索 x 和 y,而不是为每个单独的 getter,所以调用者会看到一致的值,但为什么是私有(private)构造函数?这里有什么诀窍
最佳答案
这里已经有很多答案,但我真的很想深入了解一些细节(就我的知识而言,让我吧)。我强烈建议您运行答案中出现的每个示例,以亲自了解事情的发生方式和原因。
要了解解决方案,首先要了解问题。
假设 SafePoint 类实际上如下所示:
class SafePoint {
private int x;
private int y;
public SafePoint(int x, int y){
this.x = x;
this.y = y;
}
public SafePoint(SafePoint safePoint){
this(safePoint.x, safePoint.y);
}
public synchronized int[] getXY(){
return new int[]{x,y};
}
public synchronized void setXY(int x, int y){
this.x = x;
//Simulate some resource intensive work that starts EXACTLY at this point, causing a small delay
try {
Thread.sleep(10 * 100);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.y = y;
}
public String toString(){
return Objects.toStringHelper(this.getClass()).add("X", x).add("Y", y).toString();
}
}
什么变量创建了这个对象的状态?只有两个:x,y。它们是否受到某种同步机制的保护?好吧,它们是通过内在锁,通过 synchronized 关键字 - 至少在 setter 和 getter 中。他们在其他任何地方都被“感动”了吗?当然在这里:
public SafePoint(SafePoint safePoint){
this(safePoint.x, safePoint.y);
}
你在这里所做的是读取你的对象。对于一个线程安全的类,你必须协调对它的读/写访问,或者在同一个锁上同步。但是这里没有发生这样的事情。 setXY 方法确实是同步的,但是克隆构造函数不是,因此调用这两个可以以非线程安全的方式完成。我们可以刹车吗?
让我们试试这个:
public class SafePointMain {
public static void main(String[] args) throws Exception {
final SafePoint originalSafePoint = new SafePoint(1,1);
//One Thread is trying to change this SafePoint
new Thread(new Runnable() {
@Override
public void run() {
originalSafePoint.setXY(2, 2);
System.out.println("Original : " + originalSafePoint.toString());
}
}).start();
//The other Thread is trying to create a copy. The copy, depending on the JVM, MUST be either (1,1) or (2,2)
//depending on which Thread starts first, but it can not be (1,2) or (2,1) for example.
new Thread(new Runnable() {
@Override
public void run() {
SafePoint copySafePoint = new SafePoint(originalSafePoint);
System.out.println("Copy : " + copySafePoint.toString());
}
}).start();
}
}
输出很容易就是这个:
Copy : SafePoint{X=2, Y=1}
Original : SafePoint{X=2, Y=2}
这是逻辑,因为一个线程更新=写入我们的对象,另一个正在读取它。它们不会在某些公共(public)锁上同步,因此不会同步输出。
解决方案?
同步构造函数,这样读取将在同一个锁上同步,但是Java中的构造函数不能使用同步关键字——这当然是逻辑。
可能会使用不同的锁,比如可重入锁(如果同步关键字不能使用)。但它也不起作用,因为构造函数中的第一条语句必须是对 this/super 的调用。如果我们实现不同的锁,那么第一行必须是这样的:
lock.lock()//这里的lock是ReentrantLock,编译器不会因为上面的原因允许这个。
如果我们让构造函数成为一个方法呢?当然可以!
查看此代码示例
/*
* this is a refactored method, instead of a constructor
*/
public SafePoint cloneSafePoint(SafePoint originalSafePoint){
int [] xy = originalSafePoint.getXY();
return new SafePoint(xy[0], xy[1]);
}
调用看起来像这样:
public void run() {
SafePoint copySafePoint = originalSafePoint.cloneSafePoint(originalSafePoint);
//SafePoint copySafePoint = new SafePoint(originalSafePoint);
System.out.println("Copy : " + copySafePoint.toString());
}
这一次代码按预期运行,因为读取和写入同步在同一个锁上,但是我们删除了构造函数。如果不允许这样做呢?
我们需要找到一种方法在同一个锁上同步读取和写入 SafePoint。
理想情况下,我们会想要这样的东西:
public SafePoint(SafePoint safePoint){
int [] xy = safePoint.getXY();
this(xy[0], xy[1]);
}
但编译器不允许这样做。
我们可以通过调用 *getXY 方法来安全地读取,因此我们需要一种方法来使用它,但是我们没有一个构造函数来接受这样的参数 - 创建一个。
private SafePoint(int [] xy){
this(xy[0], xy[1]);
}
然后,实际的调用:
public SafePoint (SafePoint safePoint){
this(safePoint.getXY());
}
注意构造函数是私有(private)的,这是因为我们不想暴露另一个公共(public)构造函数并再次考虑类的不变量,因此我们将其设为私有(private) - 只有我们可以调用它。
关于java - 避免竞争条件的私有(private)构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12028925/
假设我正在使用 APC,其中过程和调用代码都使用 SetLastError 和 GetLastError。这会导致 GetLastError 产生不可预测的值。有什么办法可以解决这个问题吗? VOID
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 7年前关闭。 Improve t
任何人都可以,请告诉我,如何在不进行JavaScript轮询/ setInterval的情况下,在完整日历上填充/显示在服务器端动态更新的数据。 grails中提供了Atmosphere插件,但是文档
我正在尝试调整我的代码,从仅在前台使用 WCSessionDelegate 回调到在后台通过 handleBackgroundTasks: 接受 WKWatchConnectivityRefreshB
我正在构建批处理系统。 单位 的批处理数量从 20 到 1000 不等。每个 Unit 本质上都是模型的层次结构(一个主模型和许多子模型)。我的任务涉及将每个模型层次结构作为单个事务保存到数据库中(每
我拍了一张图片并将其切成三 block ,然后将它们向右浮动,让文字围绕它们流动。 HTML 看起来像这样: 在我添加侧边栏并将其 float 到图像的右上方之前,它工作正常,就像这样... T
我正在考虑嵌入式 Linux 项目(还没有硬件)中即将出现的情况,其中两个外部芯片需要共享一条物理 IRQ 线。这条线在硬件中能够实现边沿触发,但不能实现电平触发中断。 查看 Linux 中的共享 i
我观察到,当 linux futexes 发生争用时,系统会在自旋锁上花费大量时间。我注意到即使不直接使用 futex 也是一个问题,但在调用 malloc/free、rand、glib 互斥调用和其
我终于能够获得一些工具提示,最终可以使用以下代码: Hover over me 然后 $('[rel=tooltip]').tooltip(); 我遇到的问题是它使用 jQueryUI 工
我是一名优秀的程序员,十分优秀!