Java并行开发笔记2

可变的Point类

@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;
   }
}

 不可变的Point类

@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;
    }
}

 

 

你可能感兴趣的:(java,并行开发)