NodeSelectorSlot类中,向map中加入新的DefaultNode时,为什么不是直接通过

Type: feature request Thank you for your attention,I have a question to ask. the code is following: private volatile Map<String, DefaultNode> map = new HashMap<String, DefaultNode>(10); public void entry(Context context, ResourceWrapper resourceWrapper, Object obj, int count, boolean prioritized, Object... args) throws Throwable { DefaultNode node = map.get(context.getName()); if (node == null) { synchronized (this) { node = map.get(context.getName()); if (node == null) { node = new DefaultNode(resourceWrapper, null); HashMap<String, DefaultNode> cacheMap = new HashMap<String, DefaultNode>(map.size()); cacheMap.putAll(map); cacheMap.put(context.getName(), node); map = cacheMap; } // Build invocation tree ((DefaultNode)context.getLastNode()).addChild(node); } } context.setCurNode(node); fireEntry(context, resourceWrapper, node, count, prioritized, args); } When node == null, we will new a {@link DefaultNode} and add to map.Your mathod is building an object named cacheMap first,and than assigning it to map.Why not put the node to map directly as follow: map.put(context.getName(), node);,What are the considerations?

当 node == null 的时候,我们通过新建一个DefaultNode加入到map中,代码中的方法是先定义一个cacheMap对象,然后将map原有的值以及新建的node加入到cacheMap中。为什么不直接通过map.put(context.getName(), node);将node加入到map呢,是出于什么考虑才这样做的?

原提问者GitHub用户xgxizz

展开
收起
学习娃 2023-05-19 15:58:22 74 发布于北京 分享
分享
版权
举报
1 条回答
写回答
取消 提交回答
  • 为了线程安全,使用了 copy on write

    原回答者GitHub用户liqiangz

    2023-05-19 20:48:24 举报
    赞同 评论

    评论

    全部评论 (0)

    登录后可评论
问答标签:
问答地址:
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等