多线程经典问题--哲学家就餐问题解决方案JAVA实现

哲学家就餐问题作为多线程经典问题中其中一个,解决方案有多种。其中下面介绍的方案是:哲学家轮番判断左右手的筷子是否处于可用状态,如果两边都可用,则拿起筷子;其中任意一边不可用,则阻塞哲学家线程。

public class Philosopher implements Runnable{

	private Fork fork;
	
	private String name;
	
	public Philosopher(String name,Fork fork){
		this.name = name;
		this.fork = fork;
	}
	
	@Override
	public void run() {
		// TODO Auto-generated method stub
		while(true){
			thinking();
			fork.takeFork(name);
			eating();
			fork.putDownFork(name);
		}
	}
	
	private void thinking(){
		try {
			System.out.println("哲学家在思考");
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	private void eating(){
		try {
			System.out.println("哲学家在吃饭");
			Thread.sleep(1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String[] args) {
		Fork fork = new Fork();
		Philosopher p1 = new Philosopher("0",fork);
		Philosopher p2 = new Philosopher("1",fork);
		Philosopher p3 = new Philosopher("2",fork);
		Philosopher p4 = new Philosopher("3",fork);
		Philosopher p5 = new Philosopher("4",fork);
		new Thread(p1).start();
		new Thread(p2).start();
		new Thread(p3).start();
		new Thread(p4).start();
		new Thread(p5).start();
	}

}


class Fork{
	
	private boolean[] isUsed = {false,false,false,false,false};
	
	public synchronized void takeFork(String name){
		int i = Integer.valueOf(name);
		while(isUsed[i]||isUsed[(i+1)%5]){
			try {
				wait();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		notifyAll();
		System.out.println("哲学家"+name+":拿起了筷子"+i+"和"+(i+1)%5);
		isUsed[i]=isUsed[(i+1)%5]=true;
	}
	
	public synchronized void putDownFork(String name){
		int i = Integer.valueOf(name);
		System.out.println("哲学家"+name+":放下了筷子"+i+"和"+(i+1)%5);
		isUsed[i]=isUsed[(i+1)%5]=false;
		notifyAll();
	}
	
	
}


 


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值