哲学家吃饭问题(资源加锁和超时释放)

本文通过两个具体的Java线程示例,展示了资源锁定机制如何防止死锁,并介绍了通过超时来解决潜在死锁问题的方法。第一个示例使用简单的同步块实现哲学家就餐问题,第二个示例则引入了等待通知机制并设置了超时来避免永久等待。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

public class Resourcelocking extends Thread{

    private static int[] chopstick = { 1, 1, 1, 1, 1 };
    private int i;

    public Resourcelocking(int i) {
        this.i = i;
    }

    @Override
    public void run() {

        //synchronized (chopstick) {  //若注释此行,打开下行,不同步,5个per只拿到左筷子
            {
            eat(this.getName()); //得到线程的名字

            think(this.getName());
        }

    }

    private void think(String name) {
        //在思考的时候把筷子置为可用
        chopstick[i] = 1;
        chopstick[(i + 1) % 5] = 1;
        System.out.println("per"+name+" is thinking...");

    }

    private void eat(String string) {

        while (true) {

            if (chopstick[i] != 0) {//说明当前现这根筷子没有被占用
                chopstick[i]--;
                System.out.println("per" + this.getName()
                        + " got left chopstick.");
                break;
            }
        }

        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //拿到左筷子后开始拿右筷子
        while (true) {
            if (chopstick[(i + 1) % 5] != 0) {
                chopstick[(i + 1) % 5]--;
                System.out.println("per" + this.getName()
                        + " got right chopstick.");
                break;
            }

        }
        System.out.println("per" + string + " is eating...");
    }
}


public class Timeoutrelease extends Thread{
    private static int[] chopstick = {1,1,1,1,1};
    private int i;
    private int n = 5;

    public Timeoutrelease(int i) {
        this.i = i;
    }

    @Override
    public void run() {

        //拿左筷子
        synchronized (chopstick) {
            while (chopstick[i] == 0) {
                try {
                    chopstick.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }


            chopstick[i]--;
            System.out.println("per" + this.getName()
                    + " got left chopstick.");
            chopstick.notify();
        }

        // 睡一下产生死锁
        try {
            Thread.sleep((long) (Math.random()*1000));
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //拿右筷子
        synchronized (chopstick) {
            while (chopstick[(i + 1) % n] == 0) {
                try {
                    chopstick.wait(3*1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                finally{
                    System.out.println(this.getName()+" waited 3s ,free left chopstick");
                    chopstick[i] ++;
                }
            }

            chopstick[(i + 1) % n]--;
            System.out.println("per" + this.getName()
                    + " got right chopstick.");

            System.out.println("per" + this.getName() + " is eating...");

            chopstick[i]++;
            chopstick[(i + 1) % n]++;
            System.out.println("per"+this.getName()+" is thinking...");

            chopstick.notify();
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值