java 多线程 避免死锁 哲学家就餐问题

2 篇文章 0 订阅
2 篇文章 0 订阅

这里写图片描述
这里写图片描述
源代码如下:

public class Concurrence implements Runnable{
    private chick pre;
    private chick last;
    private int index;

    public Concurrence(chick pre, chick last, int index) {
        this.pre = pre;
        this.last = last;
        this.index = index;
    }

    public void eating(){
        System.out.println("pre"+index+":eating......");
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    public void thinking(){
        System.out.println("pre"+index+":thinking......");
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        while (true){
                if(pre.isSatuts()){
                    synchronized (pre) {
                        pre.setSatuts(false);
                        if (last.isSatuts()) {
                            synchronized (last) {
                                last.setSatuts(false);
                                eating();
                                pre.setSatuts(true);
                                last.setSatuts(true);
                                last.notifyAll();
                            }
                        } else {
                            thinking();
                        }
                        thinking();
                        try {
                           pre.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }else {
                    thinking();
                }
                thinking();
        }

    }
}
class chick{
    private int index;
    private boolean satuts=true;
    public chick(int index,boolean satuts) {
        this.index = index;
        this.satuts = satuts;
    }
    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public synchronized boolean isSatuts() {
        return satuts;
    }

    public synchronized void setSatuts(boolean satuts) {
        this.satuts = satuts;
    }
}
public static void main(String[] args) throws InterruptedException {
        chick c1=new chick(1,true);
        chick c2=new chick(2,true);
        chick c3=new chick(3,true);
        chick c4=new chick(4,true);
        chick c5=new chick(5,true);
        Concurrence con1=new Concurrence(c1,c2,1);
        Concurrence con2=new Concurrence(c2,c3,2);
        Concurrence con3=new Concurrence(c3,c4,3);
        Concurrence con4=new Concurrence(c4,c5,4);
        Concurrence con5=new Concurrence(c5,c1,5);
        new Thread(con1).start();
        new Thread(con2).start();
        new Thread(con3).start();
        new Thread(con4).start();
        new Thread(con5).start();
        }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
哲学家就餐问题是一个经典的并发编程问题,用来展示多线程编程中的死锁和竞争条件等问题。下面是一个使用C++多线程解决哲学家就餐问题的示例代码: ```c++ #include <iostream> #include <thread> #include <mutex> using namespace std; const int kNumPhilosophers = 5; // 哲学家数量 mutex forks[kNumPhilosophers]; // 叉子锁 void philosopher(int id) { int left_fork = id; int right_fork = (id + 1) % kNumPhilosophers; // 模拟哲学家思考 cout << "Philosopher " << id << " is thinking." << endl; this_thread::sleep_for(chrono::seconds(1)); // 尝试获得叉子 cout << "Philosopher " << id << " is hungry and wants to eat." << endl; forks[left_fork].lock(); forks[right_fork].lock(); // 开始就餐 cout << "Philosopher " << id << " is eating." << endl; this_thread::sleep_for(chrono::seconds(1)); // 释放叉子 forks[right_fork].unlock(); forks[left_fork].unlock(); // 就餐结束 cout << "Philosopher " << id << " finished eating and is thinking again." << endl; } int main() { // 创建哲学家线程 thread philosophers[kNumPhilosophers]; for (int i = 0; i < kNumPhilosophers; i++) { philosophers[i] = thread(philosopher, i); } // 等待所有哲学家线程结束 for (int i = 0; i < kNumPhilosophers; i++) { philosophers[i].join(); } return 0; } ``` 在这个示例代码中,我们使用了`mutex`来实现叉子的锁机制,每个哲学家线程都会先尝试获得左边和右边的叉子,如果获得成功就开始就餐,否则就会等待。这个示例代码简单易懂,但是仍然存在死锁和竞争条件等问题,需要进一步的优化和改进。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值