@ThreadSafe public class PersonSet { @GuardedBy("this") private final Set<Person> mySet = new HashSet<Person>(); public synchronized void addPerson(Person p) { mySet.add(p); } public synchronized boolean containsPerson(Person p) { return mySet.contains(p); } }
public class PrivateLock{ private final Object myLock = new Object(); @GuardedBy("myLock") Widget widget; void someMethod() { synchronized(myLock) { //访问或修改Widget的状态 } } }
@Immutable public class Point { public final int x, y ; public Point(int x, int y) { this.x = x; this.y = y; } } @ThreadSafe public class DelegatingVehicleTracker { private final ConcurrentMap<String, Point> locations; public DelegatingVehicleTracker(Map<String, Point> points) { locations = new ConcurrentHashMap<String, Point>(points); } public Map<String, Point> getLocations() { return Collections.unmodifiableMap ( new HashMap<String, Point>(locations) }; } public Point getLocation(String id) { return locations.get(id); } public void setLocation(String id, int x, int y) { if(location.replace(id, new Point(x, y) == null ) throw new IllegalArgumentException( "invalid vechicle name: " + id); } }
public class NumberRange { //不变性条件:lower <= upper private final AtomicInteger lower = new AtomicInteger(0); private final AtomicInteger upper = new AtomicInteger(0); public void setLower(int i) { //注意:不安全的"先检查后执行" if( i > upper.get() ) { throw new IllegalArgumentException( "can't set lower to " + i + " > upper"); } lower.set(i); } public void setUpper(int i) { if(i < lower.get()) throw new IllegalArgumentException( "can't set upper to " + i + " < lower"); } public boolean isInRange(int i) { return ( i >= lower.get() && i <= upper.get( ) ); } }
@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; } } @ThreadSafe public class PublishingVehicleTracker { private final ConcurrentMap<String, Point> locations; public DelegatingVehicleTracker(Map<String, Point> points) { locations = new ConcurrentHashMap<String, Point>(points); } public Map<String, Point> getLocations() { return Collections.unmodifiableMap ( new HashMap<String, Point>(locations) }; } public Point getLocation(String id) { return locations.get(id); } public void setLocation(String id, int x, int y) { if(!location.containsKey(id) ) throw new IllegalArgumentException( "invalid vechicle name: " + id); locations.get(id).set(x, y); } }
@ThreadSafe public class BetterVector<E> extends Vector<E> { public synchronized boolean putIfAbsent(E x) { boolean absent = !contains(x); if(absent) add(x); return absent; } }
@ThreadSafe public class ListHelper<E> { public List<E> list = Collections.synchronizedList( new ArrayList<E>( ) ); ... public boolean putIfAbsent(E x) { synchronized(list) { boolean absent = !list.contains(x); if(absent) list.add(x); return absent; } } }
@ThreadSafe public class ImprovedList<T> implements List<T> { private final List<T> list; public ImprovedList(List<T> list) { this.list = list;} public synchronized boolean putIfAbsent(T x) { boolean contains = list.contains(x); if(contains) list.add(x); return !contains; } public synchronized void clear() { list.clear( ) ;} //...按照类似的方式委托List的其他方法 }
原文地址:http://blog.csdn.net/troy__/article/details/38798337