Would I still hold reference to the owner of the Consumer if the listener that triggers the Consumer is enclosed by a WeakReference?

huangapple go评论75阅读模式
英文:

Would I still hold reference to the owner of the Consumer if the listener that triggers the Consumer is enclosed by a WeakReference?

问题

像这样:

Proc 是一个无返回值的函数式接口类型。

public class MainViewPagerViewModel extends ViewModel {

    private HashMap<Integer, Integer> quantities = new HashMap<>();
    private WeakReference<Proc> weakProc;

    public void put(Integer position, Integer quantity) {
        quantities.put(position, quantity);
        if (weakProc.get() != null) {
            weakProc.get().run();
        }
    }

    public void updatePieChart(Consumer<HashMap<Integer, Integer>> mapConsumer) {
        weakProc = new WeakReference<>(
                () -> mapConsumer.accept(quantities)
        );
    }
}

这样会起作用吗?还是我仍然持有对 Consumer 的引用?或者...是不是太多了?也许这已经足够了?

public void updatePieChart(Consumer<HashMap<Integer, Integer>> mapConsumer) {
    proc = () -> mapConsumer.accept(quantities);
}

提前感谢您。

英文:

Like this:

Proc is a functional interface type void.

public class MainViewPagerViewModel extends ViewModel {

    private HashMap&lt;Integer, Integer&gt; quantities = new HashMap&lt;&gt;();
    private WeakReference&lt;Proc&gt; weakProc;

    public void put(Integer position, Integer quantity) {
        quantities.put(position, quantity);
        if (weakProc.get() != null) {
            weakProc.get().run();
        }
    }

    public void updatePieChart(Consumer&lt;HashMap&lt;Integer, Integer&gt;&gt; mapConsumer) {
        weakProc = new WeakReference&lt;&gt;(
                () -&gt; mapConsumer.accept(quantities)
        );
    }

}

Will this work, or I'm I still holding reference to the Consumer's onwer? or... is this too much? maybe this is enough?

    public void updatePieChart(Consumer&lt;HashMap&lt;Integer, Integer&gt;&gt; mapConsumer) {
        
            proc = () -&gt; mapConsumer.accept(quantities)
      
    }

Thanks in advance.

答案1

得分: 1

首先,有一个非常一般的建议需要放在这里:除非你知道某些构造的作用(在这种情况下是WeakReference),否则不要使用它。研究它的文档,运行不止一个示例,当你对它感到舒适时再使用它。WeakReference是一把锋利的工具,你需要了解可能遇到的所有问题和问题。关于它是什么的一些介绍在这里或者在这里,甚至可能还在这里

现在 - 你具体想解决什么问题?如果没有围绕Proc的包装器private WeakReference&lt;Proc&gt; weakProc;,你的代码是否有问题?我认为没有。

通过引入那个WeakReference,就好像你在说Proc(它是一个作为lambda表达式定义的Runnable)会在MainViewPagerViewModel仍然存在时被GC回收。现在问问自己 - 这甚至可能吗?在某个时刻,proc可以被垃圾回收,而MainViewPagerViewModel仍然存在吗?如果是这种情况 - 当你调用put时会发生什么?

当你得到上面的答案时,你将明白你的问题在某种程度上没有多大意义;而在那里引入WeakReference是没有用的。

英文:

First of all, there is a very general advice that needs to be in place here: unless you know what some constructs do (WeakReference in this case), don't use it. Study it's documentation, run more than just one example and when you are comfortable with it - use it. WeakReferences are a sharp tool, and you need to know all the problems and issue you might encounter. Here are a few intros about what it is here, or here or may be even here.

And now - what are you trying to solve, specifically? Is your code broken without that wrapper around Proc: private WeakReference&lt;Proc&gt; weakProc;? I don't think it is.

By introducing that WeakReference it's like you are saying that Proc (which is a Runnable defined as a lambda expression) will be GCed, while the MainViewPagerViewModel is still alive. And now ask yourself - is that even possible? Forget about WeakReference here for a second. Can proc be garbage collected at some point in time while MainViewPagerViewModel is still going to be alive? If that is the case - what would happen when you would call put?

When you have the answer to the above, you will understand that your question makes somehow very little sense; and the introduction of WeakReference there is useless.

huangapple
  • 本文由 发表于 2020年9月10日 10:28:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/63821968.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定