一、synchronized 加锁版
筷子
public class Chopstick {
private String name;
public Chopstick(String name) {
this.name = name;
}
@Override
public String toString() {
return "Chopstick{" +
"name='" + name + '\'' +
'}';
}
}
哲学家
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
public class Philosopher extends Thread {
final Chopstick left;
final Chopstick right;
public Philosopher(String name, Chopstick left, Chopstick right) {
super(name);
this.left = left;
this.right = right;
}
@Override
public void run() {
while (true) {
synchronized (left) {
synchronized (right) {
eat();
}
}
fight();
}
}
public void eat(){
System.out.println(this.getName()+":吃饭");
try {
TimeUnit.MICROSECONDS.sleep(ThreadLocalRandom.current().nextInt(100));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void fight(){
System.out.println(this.getName()+":摔跤");
try {
TimeUnit.MICROSECONDS.sleep(ThreadLocalRandom.current().nextInt(100));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
测试类
public class PhilosopherTest1 {
public static void main(String[] args) {
Chopstick a = new Chopstick("a");
Chopstick b = new Chopstick("b");
Chopstick c = new Chopstick("c");
Chopstick d = new Chopstick("d");
Chopstick e = new Chopstick("e");
new Philosopher("Banana", a, b).start();
new Philosopher("Rossett", b, c).start();
new Philosopher("Van", c, d).start();
new Philosopher("Billy", d, e).start();
new Philosopher("Biollante", e, a).start();
//new Philosopher("Biollante", a, e).start();//改变顺序打破死锁
}
}
二、ReentranLock谦让版。
筷子
import java.util.concurrent.locks.ReentrantLock;
public class Chopstick2 extends ReentrantLock {
private final String name;
public Chopstick2(String name) {
this.name = name;
}
@Override
public String toString() {
return name;
}
}
哲学家
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
public class Philosopher2 extends Thread {
Chopstick2 left;
Chopstick2 right;
public Philosopher2(String name, Chopstick2 left, Chopstick2 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();
}
}
fight();
}
}
public void eat(){
System.out.println(this.getName()+":吃饭");
try {
TimeUnit.MICROSECONDS.sleep(ThreadLocalRandom.current().nextInt(100));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void fight(){
System.out.println(this.getName()+":摔跤");
try {
TimeUnit.MICROSECONDS.sleep(ThreadLocalRandom.current().nextInt(100));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
测试类
public class PhilosopherTest2 {
public static void main(String[] args) {
Chopstick2 a = new Chopstick2("a");
Chopstick2 b = new Chopstick2("b");
Chopstick2 c = new Chopstick2("c");
Chopstick2 d = new Chopstick2("d");
Chopstick2 e = new Chopstick2("e");
new Philosopher2("Banana", a, b).start();
new Philosopher2("Rossett", b, c).start();
new Philosopher2("Van", c, d).start();
new Philosopher2("Billy", d, e).start();
new Philosopher2("Biollante", a, b).start();
}
}