例1、不用线程锁的情况下,两个线程对同一个变量进行加减操作
static void Main(string[] args) { Counter counter = new Counter(); var t1 = new Thread(() => TestCounter(counter)); var t2 = new Thread(() => TestCounter(counter)); t1.Start(); t2.Start(); Thread.Sleep(TimeSpan.FromSeconds(3));//睡眠3秒,保证t1、t2两个线程都运行完毕 Console.WriteLine(counter.count); Console.Read(); }
对count变量不断地加1减1,最后count应该为0,但可以看出用两个线程来进行这个操作的时候,往往得到的结果并不是0,出现错误。
例2、使用线程锁,两个线程对同一个变量进行加减操作
static void Main(string[] args) { CounterWithLock counterlock = new CounterWithLock(); var t1 = new Thread(() => TestCounter(counterlock)); var t2 = new Thread(() => TestCounter(counterlock)); t1.Start(); t2.Start(); Thread.Sleep(TimeSpan.FromSeconds(3));//睡眠3秒,保证t1、t2两个线程都运行完毕 Console.WriteLine(counterlock.count); Console.Read(); }
加上线程锁后,结果正常,因此加上线程锁才能保证不会出错。
例1、例2所用到的类和方法
/// <summary> /// 对同一个变量进行1000次加减操作 /// </summary> /// <param name="c"></param> static void TestCounter(CounterBase c) { Console.WriteLine("TestCounter start"); for (int i = 0; i < 1000; i++) { c.Increment(); c.Decrement(); } Console.WriteLine("TestCounter end"); }
abstract class CounterBase { public abstract void Increment(); public abstract void Decrement(); } /// <summary> /// 没有锁 /// </summary> class Counter : CounterBase { public int count { get; private set; } public override void Increment() { count ++; } public override void Decrement() { count --; } } /// <summary> /// 加锁 /// </summary> class CounterWithLock : CounterBase { private readonly object locker = new object(); public int count { get; private set; } public override void Increment() { lock (locker) { count++; } } public override void Decrement() { lock (locker) { count--; } } }
时间: 2024-10-20 12:01:11