有五个哲学家,他们共用一张圆桌,分别坐在五张椅子上。在圆桌上五支筷子,平时一个哲学家进行思考,饥饿时便试图取用其左、右最靠近他的筷子,只有在他拿到两支筷子时才能进餐。进餐完毕,放下筷子又继续思考。
代码模拟
public class Philosopher extends Thread{ private ChopStick left, right; private int index; public Philosopher(ChopStick left, ChopStick right, int index) { this.left = left; this.index = index; this.right = right; } @Override public void run() { synchronized (left) { try { Thread.sleep(3000L); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(index + "等待right筷子"); synchronized (right) { System.out.println(index + "拿到right筷子,吃饭"); } } } public static void main(String[] args) { ChopStick chopsticks1 = new ChopStick(); ChopStick chopsticks2 = new ChopStick(); ChopStick chopsticks3 = new ChopStick(); ChopStick chopsticks4 = new ChopStick(); ChopStick chopsticks5 = new ChopStick(); String mutex="lock"; Philosopher philosopherThread1 = new Philosopher(chopsticks1, chopsticks2, 1,mutex); Philosopher philosopherThread2 = new Philosopher(chopsticks2, chopsticks3, 2,mutex); Philosopher philosopherThread3 = new Philosopher(chopsticks3, chopsticks4, 3,mutex); Philosopher philosopherThread4 = new Philosopher(chopsticks4, chopsticks5, 4,mutex); Philosopher philosopherThread5 = new Philosopher(chopsticks5, chopsticks1, 5,mutex); philosopherThread1.start(); philosopherThread2.start(); philosopherThread3.start(); philosopherThread4.start(); philosopherThread5.start(); } }
public class Philosopher extends Thread{ private ChopStick left, right; private int index; private String mutex; public Philosopher(ChopStick left, ChopStick right, int index,String mutex) { this.left = left; this.index = index; this.right = right; this.mutex = mutex; } @Override public void run() { try { Thread.sleep(3000L); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (mutex) { System.out.println(index + "拿到两个筷子,吃饭"); } } public static void main(String[] args) { ChopStick chopsticks1 = new ChopStick(); ChopStick chopsticks2 = new ChopStick(); ChopStick chopsticks3 = new ChopStick(); ChopStick chopsticks4 = new ChopStick(); ChopStick chopsticks5 = new ChopStick(); String mutex="lock"; Philosopher philosopherThread1 = new Philosopher(chopsticks1, chopsticks2, 1,mutex); Philosopher philosopherThread2 = new Philosopher(chopsticks2, chopsticks3, 2,mutex); Philosopher philosopherThread3 = new Philosopher(chopsticks3, chopsticks4, 3,mutex); Philosopher philosopherThread4 = new Philosopher(chopsticks4, chopsticks5, 4,mutex); Philosopher philosopherThread5 = new Philosopher(chopsticks5, chopsticks1, 5,mutex); philosopherThread1.start(); philosopherThread2.start(); philosopherThread3.start(); philosopherThread4.start(); philosopherThread5.start(); } }
public class Philosopher extends Thread{ private ChopStick left, right; private int index; private String mutex; public Philosopher(ChopStick left, ChopStick right, int index,String mutex) { this.left = left; this.index = index; this.right = right; this.mutex = mutex; } @Override public void run() { if(index %2 == 1){ synchronized (right) { try { Thread.sleep(3000L); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(index + "等待right筷子"); synchronized (left) { System.out.println(index + "拿到right筷子,吃饭"); } } } else { synchronized (left) { try { Thread.sleep(3000L); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(index + "等待right筷子"); synchronized (right) { System.out.println(index + "拿到right筷子,吃饭"); } } } } public static void main(String[] args) { ChopStick chopsticks1 = new ChopStick(); ChopStick chopsticks2 = new ChopStick(); ChopStick chopsticks3 = new ChopStick(); ChopStick chopsticks4 = new ChopStick(); ChopStick chopsticks5 = new ChopStick(); String mutex="lock"; Philosopher philosopherThread1 = new Philosopher(chopsticks1, chopsticks2, 1,mutex); Philosopher philosopherThread2 = new Philosopher(chopsticks2, chopsticks3, 2,mutex); Philosopher philosopherThread3 = new Philosopher(chopsticks3, chopsticks4, 3,mutex); Philosopher philosopherThread4 = new Philosopher(chopsticks4, chopsticks5, 4,mutex); Philosopher philosopherThread5 = new Philosopher(chopsticks5, chopsticks1, 5,mutex); philosopherThread1.start(); philosopherThread2.start(); philosopherThread3.start(); philosopherThread4.start(); philosopherThread5.start(); } }