Javaパラレル開発ノート2


可変ポイントクラス
@NotThreadSafe
public class MutablePoint{

   public int x,y;

   public MutablePoint(){x=0;y=0;}
   public MutablePoint(MutablePoint p){
      this.x=p.x;
      this.y=p.y;
   }
}

 可変ポイントクラス
@Immutable
public class Point{

   public final int x,y;

   public Point(int x, int y){
      this.x = x;
      this.y = y;
   }


}

 スレッドが安全で可変なPointクラス
@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;
    }
}