`
ling凌yue月
  • 浏览: 334278 次
  • 性别: Icon_minigender_1
  • 来自: 郑州
社区版块
存档分类
最新评论

哲学家吃饭问题模拟(java写的)

    博客分类:
  • java
阅读更多
这里是两种算法,公用一个chopstick类:
Chopstick类:

public class Chopstick {
	/**
	 * 此筷子是否可以拿起
	 */
	private boolean enable;
	
	/**
	 * 此筷子的名称
	 */
	public String name;
	
	public Chopstick(boolean enable, String name) {
		super();
		this.enable = enable;
		this.name = name;
	}
	
	public Chopstick(String name) {
		this(true,name);
	}
	
	public void setEnable(boolean enbl){
		this.enable = enbl;
	}
	
	public boolean getEnable(){
		return this.enable;
	}
	
	/**
	 * 拿起筷子
	 */
	public synchronized void pickup(){
		try{
			while(this.enable == false){
				wait();
			}
			this.enable = false;
		}catch(Exception e){
			
		}
	}
	
	/**
	 * 放下手中的筷子
	 */
	public synchronized void pickdown(){
		this.enable = true;
		this.notifyAll();
	}
}

Philosopher类
public class Philosopher extends Thread{

	private String name;
	
	Chopstick leftChopstick;
	Chopstick rightChopstick;
	
	public Philosopher(String name, Chopstick leftChopstick,
			Chopstick rightChopstick) {
		super();
		this.name = name;
		this.leftChopstick = leftChopstick;
		this.rightChopstick = rightChopstick;
	}

	@Override
	public void run() {
		super.run();
		leftChopstick.pickup();
		System.out.println(this.name+"拿起了左手的筷子"+leftChopstick.name);
		rightChopstick.pickup();
		System.out.println(this.name+"拿起了右手的筷子"+rightChopstick.name);
		System.out.println(this.name+"开始吃饭了");
		try {
			this.sleep(5000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		System.out.println(this.name+"吃饱了");
		rightChopstick.pickdown();
		System.out.println(this.name+"放下了右手的筷子"+this.rightChopstick.name);
		leftChopstick.pickdown();
		System.out.println(this.name+"放下了左手的筷子"+this.leftChopstick.name);
		
	}
}

Philosopher2
public class Philosopher2 extends Thread {

	private String name;

	Chopstick leftChopstick;
	Chopstick rightChopstick;

	public static int WITHOUTCHOPSTICK = 0;
	public static int WITHONECHOPSTICK = 1;
	public static int WITHTWOCHOPSTICK = 2;
	public static int withChopstickStatus = 0;

	public Philosopher2(String name, Chopstick leftChopstick,
			Chopstick rightChopstick) {
		super();
		this.name = name;
		this.leftChopstick = leftChopstick;
		this.rightChopstick = rightChopstick;
	}

	@Override
	public void run() {
		super.run();
		while (true) {
			if (this.withChopstickStatus == this.WITHOUTCHOPSTICK) {
				leftChopstick.pickup();
				System.out.println(this.name + "拿起了左手的" + leftChopstick.name);
				this.withChopstickStatus = this.WITHONECHOPSTICK;
				if (this.rightChopstick.getEnable() == true) {
					rightChopstick.pickup();
					System.out.println(this.name + "拿起了右手的"
							+ rightChopstick.name);
					this.withChopstickStatus = this.WITHTWOCHOPSTICK;
					System.out.println(this.name + "开始吃饭了");
					try {
						this.sleep(5000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
					System.out.println(this.name + "吃饱了");
					rightChopstick.pickdown();
					System.out.println(this.name + "放下了右手的"
							+ this.rightChopstick.name);
					leftChopstick.pickdown();
					System.out.println(this.name + "放下了左手的"
							+ this.leftChopstick.name);
					this.withChopstickStatus = this.WITHOUTCHOPSTICK;
					break;
				} else {
					this.leftChopstick.pickdown();
					System.out.println(this.name + "放下了左手的"
							+ this.leftChopstick.name + ",因为右手的"
							+ this.rightChopstick.name + "没法拿");
					this.withChopstickStatus = this.WITHOUTCHOPSTICK;
					try {
						this.sleep(500);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}
		}

	}

}


dining类
public class Dining {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Chopstick chopstick1 = new Chopstick("筷子 1 ");
		Chopstick chopstick2 = new Chopstick("筷子 2 ");
		Chopstick chopstick3 = new Chopstick("筷子 3 ");
		Chopstick chopstick4 = new Chopstick("筷子 4 ");
		Chopstick chopstick5 = new Chopstick("筷子 5 ");
		
		Philosopher philosopher1 = new Philosopher("哲学家 1 ", chopstick5,chopstick1);
		Philosopher philosopher2 = new Philosopher("哲学家 2 ", chopstick1,chopstick2);
		Philosopher philosopher3 = new Philosopher("哲学家 3 ", chopstick2,chopstick3);
		Philosopher philosopher4 = new Philosopher("哲学家 4 ", chopstick3,chopstick4);
		Philosopher philosopher5 = new Philosopher("哲学家 5 ", chopstick4,chopstick5);
		long startTime = System.currentTimeMillis();
		ArrayList<Thread> threads = new ArrayList<Thread>();
		threads.add(philosopher1);
		threads.add(philosopher2);
		threads.add(philosopher3);
		threads.add(philosopher4);
		threads.add(philosopher5);
		philosopher2.start();
		philosopher4.start();
		philosopher1.start();
		philosopher3.start();
		philosopher5.start();
		for (Thread thread : threads) {
			try {
				thread.join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println(System.currentTimeMillis() - startTime);
	}

}


Dining2类
public class Dining2 {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Chopstick chopstick1 = new Chopstick("筷子 1 ");
		Chopstick chopstick2 = new Chopstick("筷子 2 ");
		Chopstick chopstick3 = new Chopstick("筷子 3 ");
		Chopstick chopstick4 = new Chopstick("筷子 4 ");
		Chopstick chopstick5 = new Chopstick("筷子 5 ");
		
		Philosopher2 philosopher1 = new Philosopher2("哲学家 1 ", chopstick5,chopstick1);
		Philosopher2 philosopher2 = new Philosopher2("哲学家 2 ", chopstick1,chopstick2);
		Philosopher2 philosopher3 = new Philosopher2("哲学家 3 ", chopstick2,chopstick3);
		Philosopher2 philosopher4 = new Philosopher2("哲学家 4 ", chopstick3,chopstick4);
		Philosopher2 philosopher5 = new Philosopher2("哲学家 5 ", chopstick4,chopstick5);
		long startTime = System.currentTimeMillis();
		ArrayList<Thread> threads = new ArrayList<Thread>();
		threads.add(philosopher1);
		threads.add(philosopher2);
		threads.add(philosopher3);
		threads.add(philosopher4);
		threads.add(philosopher5);
		philosopher2.start();
		philosopher4.start();
		philosopher1.start();
		philosopher3.start();
		philosopher5.start();
		for (Thread thread : threads) {
			try {
				thread.join();
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
		}
		System.out.println(System.currentTimeMillis() - startTime);
	}

}



为了防止5位哲学家同时拿筷子,同时等或是同时放下。可以让哲学家2和4先。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics