要求开发一个程序实现如下情况:一个家庭有三个孩子,爸爸妈妈不断削苹果往盘子里面放,老大、老二、老三不断从盘子里面取苹果吃。盘子的大小有限,最多只能放5个苹果,并且爸妈不能同时往盘子里面放苹果,妈妈具有优先权。三个孩子取苹果时,盘子不能为空,三人不能同时取,老三优先权最高,老大最低。老大吃的最快,取的频率最高,老二次之。
 说明:(实体对应对象,对象-类,类中有属性和方法,类与类之间的联系跟实体与实体之间的联系一样)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsoleApplication2
{
    public class Program
    {
        //出现的问题:dish类与其他两类的对应关系
        //多线程针对的是什么,共享那块怎么处理
        static void Main(string[] args)
        {
            Dish dish = new Dish();

            PeelAnApple Mother = new PeelAnApple("Mother", dish);
            PeelAnApple Father = new PeelAnApple("Father", dish);
            EatApple child1 = new EatApple("child1", dish);
            EatApple child2 = new EatApple("child2", dish);
            EatApple child3 = new EatApple("child3", dish);
            Thread th_Father, th_Mother, th_child1, th_child2, th_child3;
            th_Mother = new Thread(new ThreadStart(Mother.Peel));
            th_Father = new Thread(new ThreadStart(Father.Peel));
            th_child1 = new Thread(new ThreadStart(child1.TimeJudge));
            th_child2 = new Thread(new ThreadStart(child2.TimeJudge));
            th_child3 = new Thread(new ThreadStart(child3.TimeJudge));
            //th_Mother.Priority = ThreadPriority.Highest;
            //th_Father.Priority = ThreadPriority.Lowest;
            //th_child1.Priority = ThreadPriority.Highest;
            //th_child2.Priority = ThreadPriority.Normal;
            //th_child3.Priority = ThreadPriority.Lowest;
            th_Mother.Start();
            th_Father.Start();
            th_child2.Start();
            th_child3.Start();
            th_child1.Start();
            Console.ReadLine();
        }
    }
    //削苹果
    public class PeelAnApple
    {
        private string Personname;
        public Dish dish;
        //构造函数
        public PeelAnApple(string name, Dish dish)
        {
            this.dish = dish;
            this.Personname = name;
        }
        public void Peel()
        {
            //循环一直在削苹果
            while (true)
            {
             //   Console.WriteLine(Personname + "在削苹果");
                Thread.Sleep(800);
                dish.PlaceOnThePlate(Personname);
            }
        }
    }
    //吃苹果
    public class EatApple
    {
        public Dish dish;
        public string EatPersonname;
        public EatApple(string name, Dish dish)
        {
            this.dish = dish;
            this.EatPersonname = name;
        }
        ///根据人判断吃的时间
        public void TimeJudge()
        {
            while (true)
            {
                switch (EatPersonname)
                {
                    case "child1":
                      
                        dish.GetFromThePlate(EatPersonname);
                        //Console.Write("线程"+Thread.CurrentThread.Name);
                        Thread.Sleep(800);
                        Console.WriteLine("child1在吃");
                        break;
                    case "child2":
                        Thread.Sleep(1000);
                        dish.GetFromThePlate(EatPersonname);
                        Console.WriteLine("child2在吃");
                        break;
                    case "child3":
                        Thread.Sleep(1200);
                        dish.GetFromThePlate(EatPersonname);
                        Console.WriteLine("child3在吃");
                        break;
                    default:
                        Console.WriteLine("到底是那一个娃在吃?");
                        break;
                }
            }
        }


    }
    //盘子
    public class Dish
    {
        public int num = 0;
        //public string Personname;
        public int AppleCount = 0;
        //public bool IFCanPlace = true;
        /// <summary>
        /// 对盘子进行操作
        /// </summary>
        /// <param name="name"></param>
        //public Dish(string name)
        //{
        //    this.Personname = name;
        //}
        public Dish()
        {
        }
        public void PlaceOnThePlate(string Personname)
        {
            //只能有一个人fangpingguo
            lock (this)
            {

                if (AppleCount > 4)
                {
                    Console.WriteLine("盘子已经放满," + Personname + "请等待");
                    Monitor.Wait(this);
                }
                else
                {
                    AppleCount++;
                    num++;
                    Console.WriteLine(Personname + "放了一个苹果,现有苹果数,:" + AppleCount);
                    Monitor.PulseAll(this);
                    
                }
                if (num > 100)
                {
                    Thread.CurrentThread.Abort();
                }
            }
        }
        public void GetFromThePlate(string Personname)
        {
            //只有一个人的时候能取
            lock (this)
            {
                if (AppleCount <= 0)
                {
                    Console.WriteLine("一个苹果没有," + Personname + "在等待");
                    Monitor.Wait(this);
                }
                else
                {
                    AppleCount--;
                    Console.WriteLine(Personname + "取走一个苹果");
                    Monitor.PulseAll(this);
                }
            }
        }
    }
}

  

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace ConsoleApplication2
{
    public class Program
    {
        //出现的问题:dish类与其他两类的对应关系
        //多线程针对的是什么,共享那块怎么处理
        static void Main(string[] args)
        {
            Dish dish = new Dish();

            PeelAnApple Mother = new PeelAnApple("Mother", dish);
            PeelAnApple Father = new PeelAnApple("Father", dish);
            EatApple child1 = new EatApple("child1", dish);
            EatApple child2 = new EatApple("child2", dish);
            EatApple child3 = new EatApple("child3", dish);
            Thread th_Father, th_Mother, th_child1, th_child2, th_child3;
            th_Mother = new Thread(new ThreadStart(Mother.Peel));
            th_Father = new Thread(new ThreadStart(Father.Peel));
            th_child1 = new Thread(new ThreadStart(child1.TimeJudge));
            th_child2 = new Thread(new ThreadStart(child2.TimeJudge));
            th_child3 = new Thread(new ThreadStart(child3.TimeJudge));
            //th_Mother.Priority = ThreadPriority.Highest;
            //th_Father.Priority = ThreadPriority.Lowest;
            //th_child1.Priority = ThreadPriority.Highest;
            //th_child2.Priority = ThreadPriority.Normal;
            //th_child3.Priority = ThreadPriority.Lowest;
            th_Mother.Start();
            th_Father.Start();
            th_child2.Start();
            th_child3.Start();
            th_child1.Start();
            Console.ReadLine();
        }
    }
    //削苹果
    public class PeelAnApple
    {
        private string Personname;
        public Dish dish;
        //构造函数
        public PeelAnApple(string name, Dish dish)
        {
            this.dish = dish;
            this.Personname = name;
        }
        public void Peel()
        {
            //循环一直在削苹果
            while (true)
            {
             //   Console.WriteLine(Personname + "在削苹果");
                Thread.Sleep(800);
                dish.PlaceOnThePlate(Personname);
            }
        }
    }
    //吃苹果
    public class EatApple
    {
        public Dish dish;
        public string EatPersonname;
        public EatApple(string name, Dish dish)
        {
            this.dish = dish;
            this.EatPersonname = name;
        }
        ///根据人判断吃的时间
        public void TimeJudge()
        {
            while (true)
            {
                switch (EatPersonname)
                {
                    case "child1":
                      
                        dish.GetFromThePlate(EatPersonname);
                        //Console.Write("线程"+Thread.CurrentThread.Name);
                        Thread.Sleep(800);
                        Console.WriteLine("child1在吃");
                        break;
                    case "child2":
                        Thread.Sleep(1000);
                        dish.GetFromThePlate(EatPersonname);
                        Console.WriteLine("child2在吃");
                        break;
                    case "child3":
                        Thread.Sleep(1200);
                        dish.GetFromThePlate(EatPersonname);
                        Console.WriteLine("child3在吃");
                        break;
                    default:
                        Console.WriteLine("到底是那一个娃在吃?");
                        break;
                }
            }
        }


    }
    //盘子
    public class Dish
    {
        public int num = 0;
        //public string Personname;
        public int AppleCount = 0;
        //public bool IFCanPlace = true;
        /// <summary>
        /// 对盘子进行操作
        /// </summary>
        /// <param name="name"></param>
        //public Dish(string name)
        //{
        //    this.Personname = name;
        //}
        public Dish()
        {
        }
        public void PlaceOnThePlate(string Personname)
        {
            //只能有一个人fangpingguo
            lock (this)
            {

                if (AppleCount > 4)
                {
                    Console.WriteLine("盘子已经放满," + Personname + "请等待");
                    Monitor.Wait(this);
                }
                else
                {
                    AppleCount++;
                    num++;
                    Console.WriteLine(Personname + "放了一个苹果,现有苹果数,:" + AppleCount);
                    Monitor.PulseAll(this);
                    
                }
                if (num > 100)
                {
                    Thread.CurrentThread.Abort();
                }
            }
        }
        public void GetFromThePlate(string Personname)
        {
            //只有一个人的时候能取
            lock (this)
            {
                if (AppleCount <= 0)
                {
                    Console.WriteLine("一个苹果没有," + Personname + "在等待");
                    Monitor.Wait(this);
                }
                else
                {
                    AppleCount--;
                    Console.WriteLine(Personname + "取走一个苹果");
                    Monitor.PulseAll(this);
                }
            }
        }
    }
}
View Code