import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.ReentrantLock;
/**
* 哲学家就餐
* Created by lcg on 2015/7/1.
*/
public class ConditionTest {
public static void main(String[] args) throws Exception {
ReentrantLock table = new ReentrantLock();
Philosopher[] philosophers = new Philosopher[7];
for(int i=0;i<philosophers.length;i++){
philosophers[i] = new Philosopher(table,i);
}
for(int i=0;i<philosophers.length;i++){
philosophers[i].setLeft(philosophers[(i+philosophers.length -1)%philosophers.length]);
philosophers[i].setRight(philosophers[(i+1)%philosophers.length]);
philosophers[i].start();
}
}
}
class Philosopher extends Thread{
private int order;
private boolean eating;
private Philosopher left;
private Philosopher right;
private ReentrantLock table;
private Condition condition;
public Philosopher(ReentrantLock table,int order){
this.table = table;
this.order = order;
eating = false;
condition = table.newCondition();
}
public void setLeft(Philosopher left) {
this.left = left;
}
public void setRight(Philosopher right) {
this.right = right;
}
@Override
public void run() {
while(true){
try {
think();
eat();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void think()throws InterruptedException{
table.lock();
try{
System.out.println("哲学家"+order+"正在思考!");
eating = false;
left.condition.signal();
right.condition.signal();
}finally {
table.unlock();
}
Thread.sleep(1000);
}
private void eat() throws InterruptedException {
table.lock();
try{
while(left.eating || right.eating){
condition.await();
}
System.out.println("哲学家"+order+"正在吃饭!");
eating = true;
}finally {
table.unlock();
}
Thread.sleep(1000);
}
}
/**
* 由于wait方法是锁的持有者,若要在单锁的情况下实现
* 则每次将唤醒所有线程,容易造成线程饿死,若要两把锁实现,则增加了同步开销
*/
class Philosopher1 extends Thread{
private int order;
private boolean eating;
private Philosopher1 left;
private Philosopher1 right;
private ReentrantLock table;
public Philosopher1(ReentrantLock table,int order){
this.table = table;
this.order = order;
eating = false;
}
public void setLeft(Philosopher1 left) {
this.left = left;
}
public void setRight(Philosopher1 right) {
this.right = right;
}
@Override
public void run() {
while(true){
try {
think();
eat();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
private void think()throws InterruptedException{
synchronized (table){
System.out.println("哲学家"+order+"正在思考!");
eating = false;
table.notify();
}
Thread.sleep(1000);
}
private void eat() throws InterruptedException {
synchronized (table){
while(left.eating || right.eating){
table.wait();
}
System.out.println("哲学家"+order+"正在吃饭!");
eating = true;
}
Thread.sleep(1000);
}
}