This is a method source code from apache zookeeper,class DataTree

/**

* update the count of this stat datanode

*

* @param lastPrefix

* the path of the node that is quotaed.

* @param diff

* the diff to be added to the count

*/

public void updateCount(String lastPrefix, int diff) {

String statNode = Quotas.statPath(lastPrefix);

DataNode node = nodes.get(statNode);

StatsTrack updatedStat = null;

if (node == null) {

// should not happen

LOG.error("Missing count node for stat " + statNode);

return;

}

synchronized (node) {

updatedStat = new StatsTrack(new String(node.data));

updatedStat.setCount(updatedStat.getCount() + diff);

node.data = updatedStat.toString().getBytes();

}

// now check if the counts match the quota

String quotaNode = Quotas.quotaPath(lastPrefix);

node = nodes.get(quotaNode);

StatsTrack thisStats = null;

if (node == null) {

// should not happen

LOG.error("Missing count node for quota " + quotaNode);

return;

}

...

My question is why it synchronized on the node object? If other thread remove the node from nodes which is a HashMap, then node becomes invalid. Is there some problem here?

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐