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

📄 dinerun.java

📁 JAVA实现的哲学家进餐问题,5个哲学家,为着一个圆桌,相互之间放着一只筷子,当哲学家饿了的时候边可拿起,傍边的筷子进餐,完了在放下
💻 JAVA
字号:
/**
 * 哲学家进餐问题 
 * 当五个人都拿到左手边筷子,都等待拿右手边筷子,则因为谁都不能放下手中的筷子,这样就进入无止境的等待,构成死锁
 * 解决方法1:奇数号先拿左边的筷子,偶数号先拿右边的筷子,即相邻两个人先拿其中间夹的筷子,使这个筷子成为临界资源;
 * 解决方法2:两边的筷子都空闲时,再拿筷子,if(chopstick.flag(n)==false&&chopstick.flag(n%5)==false);
 * 解决方法3:只允许(n-1)个人同时进餐;
 * 本程序采用方法1 解决
 * @author 龙洪(07384_15)
 *
 */
 public class DineRun {
	public static void main(String[] args){
		Chopsticks chopstick=new Chopsticks();//只产生一个Balls对象,其子类共享这个对象的属性
		Scientist0 S0=new Scientist0(chopstick);
		Scientist1 S1=new Scientist1(chopstick);
		Scientist2 S2=new Scientist2(chopstick);
		Scientist3 S3=new Scientist3(chopstick);
		Scientist4 S4=new Scientist4(chopstick);
		
		S0.start();
		S1.start();
		S2.start();
		S3.start();
		S4.start();
	}
}
class Chopsticks {
/**设置标志变量,true表示已被人拿,false表示未被人拿*/
	boolean flag0=false;
	boolean flag1=false;
	boolean flag2=false;
	boolean flag3=false;
	boolean flag4=false;
	
	//static int count=0;
	boolean s0=true;
	boolean s1=true;
	boolean s2=true;
	boolean s3=true;
	boolean s4=true;
}
class Scientist0 extends Thread{
	private Chopsticks chopstick;
	
	
	
	public Scientist0(Chopsticks c){
		this.chopstick=c;
	}
	public void run(){
		while(true){//先拿左边筷子,再拿右边筷子		
			/*
			while(chopstick.flag0==true){}//如果0号即左边筷子被拿走,则等待
			chopstick.flag0=true;         //拿走0号筷子
			while(chopstick.flag1==true){}//如果1号即右边筷子被拿走,则等待
			if(chopstick.flag0==true&&chopstick.flag1==false)//如果拿起0号左边筷子而没拿1号右边筷子,
			{
				chopstick.flag1=true;//拿起1号右边筷子
				System.out.print("哲学家0 在吃饭!");
				//try{sleep(2000);}catch(Exception e){}//可设置哲学家吃饭的时间,但在这端时间内可以看到一次有多少哲学家在吃饭,但这段时间程序会成为死锁,因此不再设置
				//chopstick.flag0 = false;//放下筷子
				//chopstick.flag1 = false;
				try{sleep(1);}catch(Exception e){}//便于观察死锁,放下球后休眠1ms
			}
			*/
			synchronized(this){
				if(chopstick.s0)
				{
					if(chopstick.flag0==false&&chopstick.flag1==false)//如果拿起0号左边筷子而没拿1号右边筷子,
					{
						chopstick.flag1=true;//拿起1号右边筷子
						chopstick.flag0=true;
						System.out.print(" 哲学家0吃饭! ");
						//try{sleep(1);}catch(Exception e){}//便于观察死锁,放下球后休眠1ms
					}
					else System.out.print(" 哲学家0思考 ");
					chopstick.s0=false;
					try{sleep(1);}catch(Exception e){}
				}
				if(!chopstick.s0&&!chopstick.s1&&!chopstick.s2&&!chopstick.s3&&!chopstick.s4)
				{
					chopstick.s0=true;//开始新一轮循环
					chopstick.s1=true;
					chopstick.s2=true;
					chopstick.s3=true;
					chopstick.s4=true;
					chopstick.flag0 = false;//放下全部筷子
					chopstick.flag1 = false;
					chopstick.flag2 = false;
					chopstick.flag3 = false;
					chopstick.flag4 = false;
					System.out.println();//换行
					System.out.println();
				}
			}
			
			//else System.out.println("哲学家0 在思考");
		}
	}
}
class Scientist1 extends Thread{
	private Chopsticks chopstick;
	public Scientist1(Chopsticks c){
		this.chopstick=c;
	}
	public void run(){
		while(true){//先拿右边筷子,再拿左边筷子
			/*
			while(chopstick.flag2==true){}
			chopstick.flag2=true;
			while(chopstick.flag1==true){}
			if(chopstick.flag2==true&&chopstick.flag1==false)
			{
				chopstick.flag1=true;
				System.out.print("哲学家1 在吃饭!");
				//try{sleep(2000);}catch(Exception e){}
				chopstick.flag2=false;
				chopstick.flag1=false;
				try{sleep(1);}catch(Exception e){}
			}*/
			//else System.out.println("哲学家1 在思考");
			synchronized(this){
				if(chopstick.s1)
				{
					if(chopstick.flag2==false&&chopstick.flag1==false)//如果拿起0号左边筷子而没拿1号右边筷子,
					{
						chopstick.flag1=true;//拿起1号右边筷子
						chopstick.flag2=true;
						System.out.print(" 哲学家1吃饭! ");
						//try{sleep(1);}catch(Exception e){}//便于观察死锁,放下球后休眠1ms
					}
					else System.out.print(" 哲学家1思考 ");
					chopstick.s1=false;
					try{sleep(1);}catch(Exception e){}
				}
				if(!chopstick.s0&&!chopstick.s1&&!chopstick.s2&&!chopstick.s3&&!chopstick.s4)
				{
					chopstick.s0=true;//开始新一轮循环
					chopstick.s1=true;
					chopstick.s2=true;
					chopstick.s3=true;
					chopstick.s4=true;
					chopstick.flag0 = false;//放下全部筷子
					chopstick.flag1 = false;
					chopstick.flag2 = false;
					chopstick.flag3 = false;
					chopstick.flag4 = false;
					System.out.println();//换行
					System.out.println();
				}
			}
			
		}
	}
}
class Scientist2 extends Thread{
	private Chopsticks chopstick;
	public Scientist2(Chopsticks c){
		this.chopstick=c;
	}
	public void run(){
		while(true){//先拿左边筷子,再拿右边筷子
			/*
			while(chopstick.flag2==true){}
			chopstick.flag2=true;
			while(chopstick.flag3==true){}
			if(chopstick.flag2==true&&chopstick.flag3==false){
				chopstick.flag3=true;
				System.out.print("哲学家2 在吃饭!!");
				//try{sleep(2000);}catch(Exception e){}
				chopstick.flag2=false;
				chopstick.flag3=false;
				try{sleep(1);}catch(Exception e){}
			}*/
			//else System.out.println("哲学家2 在思考");
			synchronized(this){
				if(chopstick.s2)
				{
					if(chopstick.flag2==false&&chopstick.flag3==false)//如果拿起0号左边筷子而没拿1号右边筷子,
					{
						chopstick.flag3=true;//拿起1号右边筷子
						chopstick.flag2=true;
						System.out.print(" 哲学家2吃饭! ");
						//try{sleep(1);}catch(Exception e){}//便于观察死锁,放下球后休眠1ms
					}
					else System.out.print(" 哲学家2思考 ");
					chopstick.s2=false;
					try{sleep(100);}catch(Exception e){}
				}
				if(!chopstick.s0&&!chopstick.s1&&!chopstick.s2&&!chopstick.s3&&!chopstick.s4)
				{
					chopstick.s0=true;//开始新一轮循环
					chopstick.s1=true;
					chopstick.s2=true;
					chopstick.s3=true;
					chopstick.s4=true;
					chopstick.flag0 = false;//放下全部筷子
					chopstick.flag1 = false;
					chopstick.flag2 = false;
					chopstick.flag3 = false;
					chopstick.flag4 = false;
					System.out.println();//换行
					System.out.println();
				}
			}
			
		}
	}
}
class Scientist3 extends Thread{
	private Chopsticks chopstick;
	public Scientist3(Chopsticks c){
		this.chopstick=c;
	}
	public void run(){
		while(true){//先拿右边筷子,再拿左边筷子
			/*
			while(chopstick.flag4==true){}
			chopstick.flag4=true;
			while(chopstick.flag3==true){}
			if(chopstick.flag4==true&&chopstick.flag3==false){
				chopstick.flag3=true;
				System.out.print("哲学家3 在吃饭!!");
				//try{sleep(2000);}catch(Exception e){}
				chopstick.flag4=false;
				chopstick.flag3=false;
				try{sleep(1);}catch(Exception e){}
			}*/
			synchronized(this){
				if(chopstick.s3)
				{
					if(chopstick.flag4==false&&chopstick.flag3==false)//如果拿起0号左边筷子而没拿1号右边筷子,
					{
						chopstick.flag4=true;//拿起1号右边筷子
						chopstick.flag3=true;
						System.out.print(" 哲学家3吃饭! ");
						//try{sleep(1);}catch(Exception e){}//便于观察死锁,放下球后休眠1ms
					}
					else System.out.print(" 哲学家3思考 ");
					chopstick.s3=false;
					try{sleep(1);}catch(Exception e){}
				}
				if(!chopstick.s0&&!chopstick.s1&&!chopstick.s2&&!chopstick.s3&&!chopstick.s4)
				{
					chopstick.s0=true;//开始新一轮循环
					chopstick.s1=true;
					chopstick.s2=true;
					chopstick.s3=true;
					chopstick.s4=true;
					chopstick.flag0 = false;//放下全部筷子
					chopstick.flag1 = false;
					chopstick.flag2 = false;
					chopstick.flag3 = false;
					chopstick.flag4 = false;
					System.out.println();//换行
					System.out.println();
				}
			}
			
		}
	}
}
class Scientist4 extends Thread{
	private Chopsticks chopstick;
	public Scientist4(Chopsticks c){
		this.chopstick=c;
	}
	public void run(){
		while(true){//先拿左边筷子,再拿右边筷子
			/*
			while(chopstick.flag4==true){}
			chopstick.flag4=true;
			while(chopstick.flag0==true){}
			if(chopstick.flag4==true&&chopstick.flag0==false){
				chopstick.flag0=true;
				System.out.print("哲学家4 在吃饭!!");
				//try{sleep(2000);}catch(Exception e){}
				chopstick.flag4=false;
				chopstick.flag0=false;
				try{sleep(1);}catch(Exception e){}
			}*/
			//else System.out.println("哲学家4 在思考");
			synchronized(this){
				if(chopstick.s4)
				{
					if(chopstick.flag4==false&&chopstick.flag0==false)//如果拿起0号左边筷子而没拿1号右边筷子,
					{
						chopstick.flag0=true;//拿起1号右边筷子
						chopstick.flag4=true;
						System.out.print(" 哲学家4吃饭! ");
						//try{sleep(1);}catch(Exception e){}//便于观察死锁,放下球后休眠1ms
					}
					else System.out.print(" 哲学家4思考 ");
					chopstick.s4=false;
					try{sleep(1);}catch(Exception e){}
				}
				if(!chopstick.s0&&!chopstick.s1&&!chopstick.s2&&!chopstick.s3&&!chopstick.s4)
				{
					chopstick.s0=true;//开始新一轮循环
					chopstick.s1=true;
					chopstick.s2=true;
					chopstick.s3=true;
					chopstick.s4=true;
					chopstick.flag0 = false;//放下全部筷子
					chopstick.flag1 = false;
					chopstick.flag2 = false;
					chopstick.flag3 = false;
					chopstick.flag4 = false;
					System.out.println();//换行
					System.out.println();
				}
			}
			
		}
	}
}

⌨️ 快捷键说明

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