我有以下代码。多种方法的C#锁定对象
public class AgeIncrementor
{
private static readonly object syncLock = new object();
public AgeIncrementor()
{
Age = 0;
}
public int Age { get; set; }
public void IncrementAge()
{
Task.Run(() =>
{
try
{
lock (syncLock)
{
Age += 10;
Console.WriteLine("Increased to {0}", Age);
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
});
}
public void Complete()
{
Console.WriteLine("Printing from Complete() method.");
}
}
和主要方法的代码如下:
class Program
{
static void Main(string[] args)
{
var ageIncrementor = new AgeIncrementor();
Console.WriteLine("Age is {0}", ageIncrementor.Age);
for (int i = 0; i < 5; i++)
{
ageIncrementor.IncrementAge();
}
ageIncrementor.Complete();
Console.WriteLine("Completed");
Console.WriteLine("Final Age is {0}", ageIncrementor.Age);
Console.ReadKey();
}
}
我意料的是,当我叫完成()方法和我期待的输出如下所有线程都将完成。
Age is 0
Increased to 10
Increased to 20
Increased to 30
Increased to 40
Increased to 50
Printing from Complete() method.
Completed
Final Age is 50
但我得到如下所示的输出。
Age is 0
Printing from Complete() method.
Completed
Final Age is 0
Increased to 10
Increased to 20
Increased to 30
Increased to 40
Increased to 50
如何确保运行IncrementAge()方法的所有线程都应该执行?
锁只是确保在多个线程不会在同一时间进入受保护的代码。我认为你必须更多地等待所有的任务完成。可能会返回任务并将其存储在数组中,然后调用['Task.WaitAll'](https://msdn.microsoft.com/en-us/library/dd270695(v = vs.110).aspx) –
我认为你需要1)将'IncrementAge()'中的任务保存到一个成员变量中,2)等待它在'Complete()中完成 或者你可以从'IncrementAge()'返回'Task'并等待使用['Task.WaitAll'](https://msdn.microsoft.com/en-us/library/dd270695(v = vs.110).aspx) – GreatAndPowerfulOz
锁定与执行顺序无关它只是防止多重访问。 –