Lazy.Force() 和 Lazy.Value 有什么区别
在 Lazy.Force
扩展方法的 MSDN 文档中说道:
强制执行该值并返回其结果。与相同 价值。互斥用于防止其他线程也 计算值。
这是否意味着它相当于使用 ExecutionAndPublication
创建一个 Lazy
实例LazyThreadSafetyMode 这样只有一个线程可以初始化实例吗?
谢谢
On the MSDN documentation for Lazy.Force<T>
extension method says:
Forces the execution of this value and returns its result. Same as
Value. Mutual exclusion is used to prevent other threads from also
computing the value.
Does it mean that it's equivalent to creating a Lazy<T>
instance with ExecutionAndPublication
LazyThreadSafetyMode so that only one thread can initialize the instance?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的。它们是相同的,并且都确保该值仅被计算一次。
Yes. They are both the same, and both make sure that the value will be computed only once.
有点奇怪的是,该方法存在并且不是一个函数 - 它不可组合并且复制
.Value
的行为。我会定义类似以下内容的内容(如F# 编译器):
It's a bit strange that the method exists and is not a function - it's not composable and replicates the behavior of
.Value
.I would define something like the following (as seen in the F# compiler):