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

针对使用两个ConcurrentHashMap的问题优化,可参考 #16

Open
liweiGe opened this issue Jul 15, 2021 · 1 comment
Open

针对使用两个ConcurrentHashMap的问题优化,可参考 #16

liweiGe opened this issue Jul 15, 2021 · 1 comment
Labels

Comments

@liweiGe
Copy link

liweiGe commented Jul 15, 2021

observerStateMap 这个map 感觉没有存在的必要,可以更加精简

private final ConcurrentHashMap<Observer<? super T>, ChangeablePair<Observer<? super T>, Boolean>> observerProxyMap = new ConcurrentHashMap();

这是我的想法,一个map就可以搞定
image

我觉得数据上会更好

另外针对这一行代码
image
改造成如下:
image

@KunMinX
Copy link
Owner

KunMinX commented Jul 15, 2021

@liweiGe

感谢你的分享和启发!

为此我的做法是,将 state 作为 ObserverProxy 的字段来管理,这样我们保留了语义的明确,没有增加学习成本,且让 Map 及其元素的使用更为简便。

private final ConcurrentHashMap<Observer<? super T>, ObserverProxy> observerMap = new ConcurrentHashMap();
@Override
    public void onChanged(T t) {
      ObserverProxy proxy = observerMap.get(target);
      if (proxy != null && proxy.state) {
        proxy.state = false;
        if (t != null || isAllowNullValue) {
          target.onChanged(t);
        }
      }
    }

目前已将本次修改的结果纳入 V6.1 更新。

@KunMinX KunMinX added the 精华 label Jul 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants