.NET 並行計算和並發10-lock鎖
阿新 • • 發佈:2017-08-22
list con ask white i++ splay read rgs none
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()); 9View CodeTask.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 { 19intlist.Add(i); 20 Thread.Sleep(300); 21 Console.WriteLine(i.ToString()); 22 } 23 } 24 } 25 26 } 27
.NET 並行計算和並發10-lock鎖