- using System;
- using System.Threading;
- namespace ThreadLock
- {
- class Program
- {
- static readonly object lockObject=new object();
- static int value1 = 2;
- static int value2 = 3;
- static void Main(string[] args)
- {
- for (int i = 0; i < 10; i++)
- {
- ThreadStart start = new ThreadStart(Add);
- new Thread(start).Start();
- Console.WriteLine("Thread {0} started", i + 1);
- }
- }
- static void Add()
- {
- lock (lockObject) {
- Thread.Sleep(100);
- value1 = value1 + value2;
- Console.WriteLine(value1);
- }
- }
- }
- }
- //该片段来自于http://www.codesnippet.cn/detail/2408201513532.html
来源: http://www.codesnippet.cn/detail/2408201513532.html