java中哲学家就餐死锁_哲学家就餐问题与死锁总结

死锁的四个条件:

(1) 互斥条件:一个资源每次只能被一个进程使用。

(2) 请求与保持条件:一个进程因请求资源而阻塞时,对已获得的资源保持不放。

(3) 不剥夺条件:进程已获得的资源,在末使用完之前,不能强行剥夺。

(4) 循环等待条件:若干进程之间形成一种头尾相接的循环等待资源关系

先写一个会造成死锁的哲学家问题。当所有哲学家同时决定进餐,拿起左边筷子时候,就发生了死锁。

public class test {

public static void main(String[] args) {

ExecutorService exec = Executors.newCachedThreadPool();

int sum = 5;

Chopstick[] chopsticks = new Chopstick[sum];

for (int i = 0; i < sum; i++) {

chopsticks[i] = new Chopstick();

}

for (int i = 0; i < sum; i++) {

exec.execute(new Philosopher(chopsticks[i], chopsticks[(i + 1) % sum]));

}

}

}

// 筷子

class Chopstick {

public Chopstick() {

}

}

cla

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值