Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DefaultKeyIdentificationChain 中的index是否会有线程安全问题? #1

Closed
phantomedc opened this issue Apr 24, 2019 · 1 comment

Comments

@phantomedc
Copy link

phantomedc commented Apr 24, 2019

您好,最近我们团队在做一些与责任链相关的coding,拜读了您的源码,深受启发,感谢!

DefaultKeyIdentificationChain.java在匹配对应的KeyIdentification时:

   List<KeyIdentification> identifications = identifyMap.get(workflowId);
    if(identifyIndexMap.containsKey(workflowId)) {
        index = identifyIndexMap.get(workflowId) == null ? 0 : identifyIndexMap.get(workflowId);
    } else {
        index = 0;
    }

    if(index == identifications.size()) return IdentificationResultType.NO_MATCH;

    KeyIdentification keyIdentification = identifications.get(index);
    index ++;

    identifyIndexMap.put(workflowId,index);

    return keyIdentification.doIdentify(identifyCriterion,this);

当多个线程同时进行doIdentify()时,线程之间的index++是否会受到干扰?望指教!我们目前在这个地方使用ThreadLocal处理,是否有这个必要?

@landy8530
Copy link
Owner

您好,最近我们团队在做一些与责任链相关的coding,拜读了您的源码,深受启发,感谢!

DefaultKeyIdentificationChain.java在匹配对应的KeyIdentification时:

   List<KeyIdentification> identifications = identifyMap.get(workflowId);
    if(identifyIndexMap.containsKey(workflowId)) {
        index = identifyIndexMap.get(workflowId) == null ? 0 : identifyIndexMap.get(workflowId);
    } else {
        index = 0;
    }

    if(index == identifications.size()) return IdentificationResultType.NO_MATCH;

    KeyIdentification keyIdentification = identifications.get(index);
    index ++;

    identifyIndexMap.put(workflowId,index);

    return keyIdentification.doIdentify(identifyCriterion,this);

当多个线程同时进行doIdentify()时,线程之间的index++是否会受到干扰?望指教!我们目前在这个地方使用ThreadLocal处理,是否有这个必要?

感谢支持,共同进步!
关于您提到的并发问题,由于我项目中并未有此场景,所以并未考虑并发问题,按我的理解,你加入TheadLocal也是可以的。不过也可以考虑使用Atomic包中的解决方案。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants