多线程之生产者---消费者模式
1 using System; 2 using System.Collections.Generic; 3 using System.Linq; 4 using System.Text; 5 using System.Net; 6 using System.Net.Sockets; 7 using System.Threading; 8 9 namespace ConsoleApplication1 10 { 11 class EatAppleSmp 12 { 13 public EatAppleSmp() 14 { 15 Thread mother, father, lada, laoer, laosan; 16 Dish dish = new Dish(this, 30); 17 productor mother1 = new productor("mother", dish); 18 productor father1 = new productor("father", dish); 19 customer lada1 = new customer("laoda", dish); 20 customer laoer1 = new customer("laoer", dish); 21 customer laosan1 = new customer("laosan", dish); 22 mother=new Thread(new ThreadStart(mother1.run)); 23 father = new Thread(new ThreadStart(father1.run)); 24 lada = new Thread(new ThreadStart(lada1.run)); 25 laoer = new Thread(new ThreadStart(laoer1.run)); 26 laosan = new Thread(new ThreadStart(laosan1.run)); 27 mother.Priority = ThreadPriority.Highest; 28 father.Priority = ThreadPriority.Normal; 29 lada.Priority = ThreadPriority.Lowest; 30 laoer.Priority = ThreadPriority.Normal; 31 laosan.Priority = ThreadPriority.Highest; 32 mother.Start(); 33 father.Start(); 34 lada.Start(); 35 laoer.Start(); 36 laosan.Start(); 37 } 38 39 } 40 class productor 41 { 42 private Dish dish; 43 private string name; 44 public productor(string name,Dish dish) 45 { 46 this.dish = dish; 47 this.name = name; 48 } 49 public void run() 50 { 51 while (true) 52 { 53 dish.put(this.name); 54 Thread.Sleep(600); 55 } 56 57 } 58 } 59 class customer 60 { 61 private Dish dish; 62 private string name; 63 public customer(string name, Dish dish) 64 { 65 this.dish = dish; 66 this.name = name; 67 } 68 public void run() 69 { 70 while (true) 71 { 72 dish.get(this.name); 73 } 74 } 75 } 76 class Dish 77 { 78 int f = 5;//可放苹果的数量 79 int total;//总共的苹果个数 80 int n = 0; 81 int j = 0; 82 EatAppleSmp app; 83 public Dish(EatAppleSmp tst, int total_apple) 84 { 85 this.total = total_apple; 86 this.app = tst; 87 88 } 89 public void put(string name) 90 { 91 if (n > total) 92 if (Thread.CurrentThread.IsAlive) 93 Thread.CurrentThread.Abort(); 94 lock (this) 95 { 96 while (f == 0) 97 { 98 try 99 { 100 Console.WriteLine(name + "正在放苹果......"); 101 Monitor.Wait(this); 102 } 103 catch(Exception e) { } 104 } 105 f--;//苹果放一个 106 Console.WriteLine(name+"放了一个苹果"); 107 n++; 108 Monitor.PulseAll(this); 109 } 110 } 111 public void get(string name) 112 { 113 if (j >= total) 114 { 115 if (Thread.CurrentThread.IsAlive) 116 Thread.CurrentThread.Abort(); 117 } 118 lock (this) 119 { 120 while (f == 5) 121 { 122 Console.WriteLine(name + "等待取苹果......"); 123 Monitor.Wait(this); 124 } 125 f++;//苹果吃一个 126 Console.WriteLine(name + "吃了一个苹果"); 127 j++; 128 129 Monitor.PulseAll(this); 130 131 } 132 } 133 } 134 class Program 135 { 136 static void Main(string[] args) 137 { 138 EatAppleSmp test = new EatAppleSmp(); 139 Console.ReadKey(); 140 } 141 } 142 }
利用lock解决多线程同步问题。题目:爸爸妈妈削苹果,三个孩子吃,盘子可装5个苹果。用到知识点monitor.wait()与monitor.plusAll();
monitor.wait()释放锁直到重新获取该对象。plusall(),告知其他线程资源已释放。