java synchronized变量_Java在局部变量图上同步(Java synchronized on local variable illustration)...
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*...
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?
更多推荐
所有评论(0)