请考虑以下代码.
static class X
{
public static int Value = Task.Run(() => 0).Result;
}
class Program
{
static void Main(string[] args)
{
var value = X.Value;
}
}
Run Code Online (Sandbox Code Playgroud)
调用Task.Run然后Result在静态初始化程序中导致程序永久冻结.为什么?
假设我有一个带静态方法的静态类.
多个线程可以同时调用此静态方法.
在这些情况下是否存在竞争条件:
Run Code Online (Sandbox Code Playgroud)a - if the method depends only on local variables b - if the method depends on local variables and member fields