示例#1
0
        static void Main(string[] args)
        {
            CustomMonitor monitor  = new CustomMonitor();
            Changer       changerA = new Changer(monitor);
            Changer       changerB = new Changer(monitor);

            List <Printer> printers = new List <Printer>
            {
                new Printer(monitor),
                new Printer(monitor),
                new Printer(monitor)
            };
            List <Thread> threads = new List <Thread>
            {
                new Thread(changerA.ChangeA),
                new Thread(changerB.ChangeB)
            };

            foreach (var p in printers)
            {
                threads.Add(new Thread(p.PrintToConsole));
            }
            for (int i = 0; i < threads.Count; i++)
            {
                threads[i].Name = "" + (i + 1);
            }
            foreach (var thread in threads)
            {
                thread.Start();
            }
            foreach (var thread in threads)
            {
                thread.Join();
            }
            Console.ReadKey();
        }
示例#2
0
 public Changer(CustomMonitor monitor)
 {
     this.monitor = monitor;
 }
示例#3
0
 public Printer(CustomMonitor monitor)
 {
     this.monitor = monitor;
 }