英文:
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<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)
);
}
}
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<HashMap<Integer, Integer>> mapConsumer) {
proc = () -> mapConsumer.accept(quantities)
}
Thanks in advance.
答案1
得分: 1
首先,有一个非常一般的建议需要放在这里:除非你知道某些构造的作用(在这种情况下是WeakReference
),否则不要使用它。研究它的文档,运行不止一个示例,当你对它感到舒适时再使用它。WeakReference
是一把锋利的工具,你需要了解可能遇到的所有问题和问题。关于它是什么的一些介绍在这里,或者在这里,甚至可能还在这里。
现在 - 你具体想解决什么问题?如果没有围绕Proc
的包装器private WeakReference<Proc> 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. WeakReference
s 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<Proc> 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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论