static void Main(string[] args) { Printer p = new Printer(); Thread[] threads = new Thread[10]; for (int i = 0; i < threads.Length; i++) { threads[i] = new Thread(p.PrintNumbers); threads[i].Name = string.Format("Worker thread #{0}", i); } foreach (Thread t in threads) { t.Start(); } Console.ReadLine(); }
static void Main(string[] args) { Console.WriteLine("***** Synchronizing Threads *****\n"); Printer p = new Printer(); // Make 10 threads that are all pointing to the same // method on the same object. Thread[] threads = new Thread[10]; for (int i = 0; i < 10; i++) { threads[i] = new Thread(new ThreadStart(p.PrintNumbers)); threads[i].Name = string.Format("Worker thread *{0}", i); } foreach (var t in threads) { t.Start(); } Console.ReadLine(); }
//使用线程池来创建线程 static void PrintNumbers(object state) { Printer printer = (Printer)state; printer.PrintNumbers(); }
static void PrintTheNumbers(object state) { Printer task = (Printer)state; task.PrintNumbers(); }