标签:index 进餐 Java ChopStick Philosopher right mutex new 多线程
哲学家进餐问题
有五个哲学家,他们共用一张圆桌,分别坐在五张椅子上。在圆桌上五支筷子,平时一个哲学家进行思考,饥饿时便试图取用其左、右最靠近他的筷子,只有在他拿到两支筷子时才能进餐。进餐完毕,放下筷子又继续思考。
代码模拟
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();
}
}
解决1 一次取所有资源
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();
}
}
解决2 奇数哲学家先取右边,偶数先取左边
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();
}
}
标签:index,进餐,Java,ChopStick,Philosopher,right,mutex,new,多线程 来源: https://www.cnblogs.com/alvin103/p/16098254.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。