以下は、ConcurrentWeakKeyHashMap.javaのisEmpty()メソッドです 。https://github.com/netty/netty/blob/master/src/main/java/org/jboss/netty/util/internal/ConcurrentWeakKeyHashMap.java
なぜmcsumが必要なのですか?if(mcsum!= 0){..}ブロックは何をしますか?
そしてもっと重要なのは、どうすれば取得できますか
if (segments[i].count != 0 || mc[i] != segments[i].modCount)
真に評価するには?
public boolean isEmpty() {
final Segment<K, V>[] segments = this.segments;
/*
* We keep track of per-segment modCounts to avoid ABA problems in which
* an element in one segment was added and in another removed during
* traversal, in which case the table was never actually empty at any
* point. Note the similar use of modCounts in the size() and
* containsValue() methods, which are the only other methods also
* susceptible to ABA problems.
*/
int[] mc = new int[segments.length];
int mcsum = 0;
for (int i = 0; i < segments.length; ++ i) {
if (segments[i].count != 0) {
return false;
} else {
mcsum += mc[i] = segments[i].modCount;
}
}
// If mcsum happens to be zero, then we know we got a snapshot before
// any modifications at all were made. This is probably common enough
// to bother tracking.
if (mcsum != 0) {
for (int i = 0; i < segments.length; ++ i) {
if (segments[i].count != 0 || mc[i] != segments[i].modCount) {
return false;
}
}
}
return true;
}
編集:ブロックが現在ConcurrentWeakKeyHashMapTest にある場合に上記を評価するコード
基本的に、1つのスレッドがconcurrentMapを継続的に監視し、別のスレッドが同じキーペア値を継続的に追加/削除します