哲学家就餐,java内置锁和显式锁方式比较

本文通过Java实现了一个经典的哲学家就餐问题,并详细解释了如何使用ReentrantLock和Condition来解决同步问题。
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);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值