What's dream?

.NET 并行计算和并发10-lock锁
  1 class Program
  2     {
  3         private static List<int> intlist;
  4         static void Main(string[] args)
  5         {
  6             intlist = new List<int>() {0};
  7             Task task1 = Task.Factory.StartNew(() => Part1());
  8             Task task2 = Task.Factory.StartNew(() => Part1());
  9             Task.WaitAll(new Task[]{task1, task2});
 10             Console.Read();
 11         }
 12 
 13         private static void Part1()
 14         {
 15             lock (intlist) //
 16             {
 17                 for (int i = intlist[intlist.Count - 1]; i < 30; i++)
 18                 {
 19                     intlist.Add(i);
 20                     Thread.Sleep(300);
 21                     Console.WriteLine(i.ToString());
 22                 }
 23             }
 24         }
 25 
 26     }
 27 
View Code

posted on 2017-08-22 11:25  kiss88  阅读(489)  评论(0编辑  收藏  举报