问题描述
哲学家问题:其中多个哲学家在共享有限资源(筷子)的情况下进行工作(思考和吃饭),这可能导致死锁。
每位哲学家需要两根筷子才能吃饭,而每根筷子只能被一位哲学家使用,这就导致了资源的互斥性。在吃饭时会拿起左右两边的筷子,并保持持有直到吃完饭。如果筷子不足,他们就会等待,在这个场景中,哲学家不会从其他哲学家手中抢夺筷子,只有当筷子可用时,他们才会拿起筷子。
可能发生所有哲学家都拿着手中的一根筷子,同时等待另一根筷子的情况,这样就形成了一个环路,每个人都在等待其他人释放资源,导致所有人都无法继续进餐,即发生了死锁。
代码演示
1. 采用synchronized写法
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import static test0202.Test02042.STATE;
public class Test02042 {
public static AtomicInteger STATE = new AtomicInteger(0b00000);
public static void main(String[] args) {
Chopstick c1 = new Chopstick("1");
Chopstick c2 = new Chopstick("2");
Chopstick c3 = new Chopstick("3");
Chopstick c4 = new Chopstick("4");
Chopstick c5 = new Chopstick("5");
new Philosopher("苏格拉底", c1, c2).start();
new Philosopher("柏拉图", c2, c3).start();
new Philosopher("亚里士多德", c3, c4).start();
new Philosopher("赫拉克利特", c4, c5).start();
new Philosopher("阿基米德", c5, c1).start();
}
}
class Philosopher extends Thread {
Chopstick left;
Chopstick right;
public Philosopher(String name, Chopstick left, Chopstick right) {
super(name);
this.left = left;
this.right = right;
}
@Override
public void run() {
while (true) {
do {
} while (!STATE.compareAndSet(STATE.get(), 0b00011));
// 尝试获得左手筷子
synchronized (left) {
// 尝试获得右手筷子
synchronized (right) {
eat();
}
}
}
}
private void eat() {
System.out.println(Thread.currentThread().getName()+"eating...");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Chopstick {
String name;
public Chopstick(String name) {
this.name = name;
}
@Override
public String toString() {
return "筷子{" + name + '}';
}
}
运行结果: 发现代码不会继续执行 ,此时已经发生了死锁
改为可以超时释放的写法:
package test0202;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;
public class Test0204 {
public static void main(String[] args) {
Chopstick c1 = new Chopstick("1");
Chopstick c2 = new Chopstick("2");
Chopstick c3 = new Chopstick("3");
Chopstick c4 = new Chopstick("4");
Chopstick c5 = new Chopstick("5");
new Philosopher("苏格拉底", c1, c2).start();
new Philosopher("柏拉图", c2, c3).start();
new Philosopher("亚里士多德", c3, c4).start();
new Philosopher("赫拉克利特", c4, c5).start();
new Philosopher("阿基米德", c5, c1).start();
}
}
class Philosopher extends Thread {
Chopstick left;
Chopstick right;
public Philosopher(String name, Chopstick left, Chopstick right) {
super(name);
this.left = left;
this.right = right;
}
@Override
public void run() {
while (true) {
// 尝试获得左手筷子
if (left.tryLock()) {
try {
// 尝试获得右手筷子
if (right.tryLock()) {
try {
eat();
} finally {
right.unlock();
}
}
} finally {
left.unlock();
}
}
}
}
private void eat() {
System.out.println(Thread.currentThread().getName()+"eating...");
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
class Chopstick extends ReentrantLock {
String name;
public Chopstick(String name) {
this.name = name;
}
@Override
public String toString() {
return "筷子{" + name + '}';
}
}