⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 class1.cs

📁 原代码详细说明是关于c++方面的希望可以帮助大家使用
💻 CS
字号:
using System;
using System.Threading;
namespace Exam7_4
{
	public class EatApple
	{
		public EatApple()
		{   
			Thread th1,th2,th3,th4,th5;
			Dish d=new Dish(this,20);
			Productor mathor=new Productor("妈妈",d);         //建立妈妈线程
			Productor fathor=new Productor("爸爸",d);         //建立爸爸线程
			Consumer one=new Consumer("老大",d,1000);         //建立老大线程
			Consumer two=new Consumer("老二",d,1200);         //建立老二线程
			Consumer three=new Consumer("老三",d,1500);       //建立老三线程
			th1=new Thread(new ThreadStart(mathor.run));
			th2=new Thread(new ThreadStart(fathor.run));
			th3=new Thread(new ThreadStart(one.run));
			th4=new Thread(new ThreadStart(two.run));
			th5=new Thread(new ThreadStart(three.run));
            th1.Priority=ThreadPriority.Highest;     //设置优先级
			th2.Priority=ThreadPriority.Normal;      //设置优先级
			th3.Priority=ThreadPriority.BelowNormal; //设置优先级
			th4.Priority=ThreadPriority.Normal;      //设置优先级
			th5.Priority=ThreadPriority.Highest;     //设置优先级
			th1.Start();
			th2.Start();
			th3.Start();
			th4.Start(); 
			th5.Start();
		}
		public static void Main(string []args)
		{
			EatApple mainframe=new EatApple();
		}
	}
	public class Dish
	{
		int f=4;     //一个盘子最多可放4个苹果
		EatApple fp;
		int num;    //可放苹果数
		int n=0;
		public Dish(EatApple fp,int num)
		{   
			this.fp=fp;                //保存EatApple对象,以便在文本框上输出信息
			this.num=num;
		}
		public void put(string name)
		{  // 取苹果方法,采用了同步控制
			lock(this)
			{
				while(f==0)
				{               //已经放满苹果,线程处于等待
					try
					{
						System.Console.WriteLine(name+"等待放苹果");
						Monitor.Wait(this);
					}
					catch(ThreadInterruptedException){}	
				}
			
				Random ra=new Random();
				int x=(int)(ra.Next(f))+1;
				f=f-x;
				n=n+x;
				System.Console.WriteLine(name+"放"+x+"个苹果");
				Monitor.PulseAll(this);
				if(n>=num) Thread.CurrentThread.Abort();
			}
		} 	
        public void get(string name)
		{   
			lock(this)
			{
				while(f==4)
				{    //没有苹果
					try
					{
						System.Console.WriteLine(name+"等待取苹果");
						Monitor.Wait(this);
					}
					catch(ThreadInterruptedException){}	
				}
				f=f+1;
				System.Console.WriteLine(name+"取苹果吃...");
				Monitor.PulseAll(this);
			}
		}	
	}
	class Productor
	{    //放苹果线程,即生产者
		private Dish d;             //放苹果的盘子
		private string name;
		public Productor(string name,Dish d)    
		{	
			this.name=name;
			this.d=d;
	    } 
		public void run()
		{
			while(true)
			{
				d.put(name);
				try
				{
					Thread.Sleep(100);
				}
				catch(ThreadInterruptedException){}
			}
		}	
	}
	public class Consumer
	{   
		private string name;
		private Dish d;             //取苹果的盘子
		private int tim;           //吃苹果所用时间
		public Consumer(string name,Dish d,int tim)
		{
			this.name=name;
			this.d=d;
			this.tim=tim;
		}
		public void run()
		{
			while(true)
			{
				d.get(name);
				try
				{
					Thread.Sleep(tim);
				}
				catch(ThreadInterruptedException){}
			}	
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -