我需要保持多個值的軌道對唯一的密鑰,即1(A,B)圖2(c,d)等...java.util.concurrent中:外部同步以去除映射值
的解決方案是由多個線程有效地訪問我有以下定義;
ConcurrentSkipListMap<key, ConcurrentSkipListSet<values>>
我的問題是當值集大小爲0時刪除鍵是否需要同步?我知道這兩個類是「併發」的,我查看了OpenJDK的源代碼,但是我看到在一個線程T1之間出現一個窗口,檢查Set是否爲空,並刪除了remove(...)另一個線程T2調用add(...)。結果是T1刪除最後一個Set條目並刪除與T2交錯的Map,只是添加一個Set條目。因此,Map和T2 Set條目被T1刪除,數據丟失。
我只是「同步」add()和remove()方法,還是有「更好」的方法?
該地圖由多個線程修改,但只能通過兩種方法修改。
代碼片段如下;
protected static class EndpointSet extends U4ConcurrentSkipListSet<U4Endpoint> {
private static final long serialVersionUID = 1L;
public EndpointSet() {
super();
}
}
protected static class IDToEndpoint extends U4ConcurrentSkipListMap<String, EndpointSet> {
private static final long serialVersionUID = 1L;
protected Boolean add(String id, U4Endpoint endpoint) {
EndpointSet endpoints = get(id);
if (endpoints == null) {
endpoints = new EndpointSet();
put(id, endpoints);
}
endpoints.add(endpoint);
return true;
}
protected Boolean remove(String id, U4Endpoint endpoint) {
EndpointSet endpoints = get(id);
if (endpoints == null) {
return false;
} else {
endpoints.remove(endpoint);
if (endpoints.size() == 0) {
remove(id);
}
return true;
}
}
}