有时,您希望线程同时共享数据。发生这种情况时,重要的是要了解代码并锁定可能出错的任何部分。下面显示了两个线程计数的简单示例。
这是一些危险的(不正确的)代码:
using System.Threading; class MainClass { static int count { get; set; } static void Main() { for (int i = 1; i <= 2; i++) { var thread = new Thread(ThreadMethod); thread.Start(i); Thread.Sleep(500); } } static void ThreadMethod(object threadNumber) { while (true) { var temp = count; System.Console.WriteLine("Thread " + threadNumber + ": Reading the value of count."); Thread.Sleep(1000); count = temp + 1; System.Console.WriteLine("Thread " + threadNumber + ": Incrementing the value of count to:" + count); Thread.Sleep(1000); } } }
您会注意到,我们没有计算1,2,3,4,5 ...,而是计算1,1,2,2,3 ...
要解决此问题,我们需要锁定count的值,以便多个不同的线程无法同时对其进行读写。通过添加锁和钥匙,我们可以防止线程同时访问数据。
using System.Threading; class MainClass { static int count { get; set; } static readonly object key = new object(); static void Main() { for (int i = 1; i <= 2; i++) { var thread = new Thread(ThreadMethod); thread.Start(i); Thread.Sleep(500); } } static void ThreadMethod(object threadNumber) { while (true) { lock (key) { var temp = count; System.Console.WriteLine("Thread " + threadNumber + ": Reading the value of count."); Thread.Sleep(1000); count = temp + 1; System.Console.WriteLine("Thread " + threadNumber + ": Incrementing the value of count to:" + count); } Thread.Sleep(1000); } } }