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
时间: 2024-11-03 22:43:29