実際のJava並行性からまっすぐ:
@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;
}
}
上記はスレッドセーフなクラスです。セッターが同期されているためです。ゲッターが x / y を個別に返さず、代わりに配列を返す理由も理解しています。2 つの質問があります。なんで ?
private SafePoint(int[] a)
public SafePoint(SafePoint p) { this(p.get()); }
それ以外のthis(p.x,p.y);