我一直在 wikipedia 上寻找解决哲学家就餐问题的方法。 资源层次解决方案
我了解它的工作原理以及打破循环结构如何防止死锁,但解决方案如何防止饥饿?一个或几个线程不能继续运行,而一些线程无法取得进展吗?
如果不是,是什么阻止了这种情况的发生?
实施:
public class DinningphilMain {
public static void main(String[] args) throws InterruptedException {
int numPhil = 3;
Philosopher[] phil = new Philosopher[numPhil];
Fork[] forkArr=new Fork[numPhil];
for (int i = 0; i < numPhil; i ++) {
forkArr[i]= new Fork(i);
}
for (int i = 0; i < numPhil-1; i++) {
phil[i]=new Philosopher(i, forkArr[i], forkArr[i+1]);
}
phil[numPhil-1]= new Philosopher(numPhil-1, forkArr[0], forkArr[numPhil-1]);
for (Philosopher p : phil)
new Thread(p).start();
}
}
这是哲学家班
import java.util.Random;
public class Philosopher implements Runnable {
int sleep = 1000;
int id;
int eatTime= 500;
Random rand = new Random();
Fork left;
Fork right;
public Philosopher(int id, Fork left, Fork right) {
this.id = id;
this.left = left;
this.right = right;
}
private void think() {
System.out.println("Philosopher " + id + " is thinking");
try {
int thinkingTime = rand.nextInt(sleep);
Thread.sleep(thinkingTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void getForks() {
System.out.println("Philosopher " + id + " is picking up forks");
try {
left.get();
right.get();
System.out.println("Philosopher " + id + " has both forks");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
private void releaseForks() {
System.out.println("Philosopher " + id + " is putting down forks");
left.release();
right.release();
}
private void eat() {
System.out.println("Philosopher " + id + " is eating");
try {
Thread.sleep(eatTime);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void run() {
while (true) {
getForks();
eat();
releaseForks();
think();
}
}
}
这是分叉类
public class Fork {
private int id;
private Thread thread;
public Fork(int id) {
this.id = id;
thread = null;
}
public int getId() {
return id;
}
public synchronized void get() throws InterruptedException {
if (thread != null)
this.wait();
thread = Thread.currentThread();
}
public synchronized void release() {
if (thread == Thread.currentThread())
thread = null;
this.notify();
}
}