@ThreadSafe public class StringSet{ private final Set<String> mySet=new HashSet<String>(); public synchronized void addString(String str){ mySet.add(str); } public synchronized boolean containsString(String str){ return mySet.contains(str); } }
@ThreadSafe public class StringSet{ private final Set<String> mySet=new HashSet<String>(); public void addString(String str){ synchronized(this){ mySet.add(str); } } public synchronized boolean containsString(String str){ boolean result=false; synchronized(this){ result=mySet.contains(str); } return result; } }
@ThreadSafe public class StringSet{ ptivate Object myLock=new Object(); private final Set<String> mySet=new HashSet<String>(); public void addString(String str){ synchronized(myLock){ mySet.add(str); } } public synchronized boolean containsString(String str){ boolean result=false; synchronized(myLock){ result=mySet.contains(str); } return result; } }
public class Point{ public final int x,y; public Point(int x,int y){ this.x=x; this.y=y; } } @ThreadSafe public class CarTracker{ private final ConcurrentMap<String,Point> locations; private final Map<String,Point> unmodifiableMap; public CarTracker(Map<String,Point> points){ locations=new ConcurrentMap<String,Point>(points); unmodifiableMap=Collections.unmodifiableMap(locations); } public Map<String,Point> getLocations(){ return unmodifiableMap; } public Point getLocation(String id){ return locations.get(id); } public void setLocation(String id,int x,int y){ if(locations.replace(id,new Point(x,y))==null) System.out.print("id not exists"); } }
public class Point{ public int x,y; public Point(int x,int y){ this.x=x; this.y=y; } public synchronized int[] get(){ return int[] (x,y); } public synchronized set(int x,int y){ this.x=x; this.y=y; } }
public class Point{ public fianl int x,y; public Point(Point p){ this.x=p.x; this.y=p.y; } } public class CarTracker{ private Map<String,Point> locations; public CarTracker(Map<String,Point> points){ locations=deepCopy(points); } public synchronized Map<String,Point> getLocations(){ return deepCopy(locations); } public synchronized Point getLocation(String id){ Point p=locations.get(id); return (p==null)?null:new Point(p); } public synchronized void setLocation(String id,int x,int y){ Point p=locations.get(id); if(p==null) System.out.print("id not exists"); p.x=x; p.y=y; } public static Map<String,Point> deepCopy(Map<String,Point> m){ Map<String,Point> result=new HashMap<String,Point>(); for(String id:m.keySet()){ result.put(id,new Point(m.get(id))); } return Collections.unmodifiable(result); } }
泡泡 2016-11-05
荡神戏魔 2019-02-24
沙落雁 2016-09-20
白诗秀儿 2016-02-01
泡泡 2016-04-01
白诗秀儿 2016-10-16
伍仔 2017-05-09
吟风 2016-06-02
沙落雁 2016-07-27
梦想天蓝 2019-01-17
藏家389 2025-03-23
藏家389 2025-03-23
藏家389 2025-03-23
藏家389 2025-03-23
藏家389 2025-03-23
藏家389 2025-03-23
藏家389 2025-03-23
藏家389 2025-03-23
藏家389 2025-03-23
藏家389 2025-03-23