《Cracking the Coding Interview》——第16章:线程与锁——题目3
2014-04-27 19:26
题目:哲学家吃饭问题,死锁问题经典模型(专门用来黑哲学家的?)。
解法:死锁四条件:1. 资源互斥。2. 请求保持。3. 非抢占。4. 循环等待。所以,某砖家拿起一只筷子后如果发现没有另一只了,就必须把手里这只筷子放下,这应该是通过破坏“请求保持”原则来防止死锁产生,请求资源失败时,连自己的资源也进一步释放,然后在下一轮里继续请求,直到成功执行。
代码:
1 // This is the class for chopsticks. 2 import java.util.concurrent.locks.Lock; 3 import java.util.concurrent.locks.ReentrantLock; 4 5 public class Chopstick { 6 private Lock lock; 7 8 public Chopstick() { 9 lock = new ReentrantLock(); 10 } 11 12 public boolean pickUp() { 13 return lock.tryLock(); 14 } 15 16 public void putDown() { 17 lock.unlock(); 18 } 19 } 20 21 //------------------------------------I'm a delimiter------------------------------------ 22 // This is the class for philosophers. 23 import java.util.Vector; 24 25 public class Philosopher extends Thread { 26 private Chopstick left; 27 private Chopstick right; 28 private int id; 29 int appetite; 30 31 final int FULL_APPETITE = 10; 32 33 public Philosopher(Chopstick left, Chopstick right, int id) { 34 // TODO Auto-generated constructor stub 35 appetite = 0; 36 this.left = left; 37 this.right = right; 38 this.id = id; 39 } 40 41 private boolean pickUp() { 42 if (!left.pickUp()) { 43 return false; 44 } 45 if (!right.pickUp()) { 46 left.putDown(); 47 return false; 48 } 49 return true; 50 } 51 52 private void putDown() { 53 left.putDown(); 54 right.putDown(); 55 } 56 57 public boolean eat() { 58 while (appetite < FULL_APPETITE) { 59 if (!pickUp()) { 60 return false; 61 } 62 System.out.println(id + ":chew~"); 63 ++appetite; 64 putDown(); 65 } 66 return appetite == FULL_APPETITE; 67 } 68 69 @Override 70 public void run() { 71 // TODO Auto-generated method stub 72 super.run(); 73 while (!eat()) { 74 // Not full yet. 75 } 76 } 77 78 public static void main(String[] args) { 79 final int n = 6; 80 Vector<Chopstick> chopsticks = new Vector<Chopstick>(); 81 Vector<Philosopher> philosophers = new Vector<Philosopher>(); 82 83 for (int i = 0; i < n; ++i) { 84 chopsticks.add(new Chopstick()); 85 } 86 for (int i = 0; i < n; ++i) { 87 philosophers.add(new Philosopher(chopsticks.elementAt(i), 88 chopsticks.elementAt((i + 1) % n), i + 1)); 89 } 90 91 for (int i = 0; i < n; ++i) { 92 philosophers.elementAt(i).start(); 93 } 94 } 95 }
转载于:https://www.cnblogs.com/zhuli19901106/p/3695085.html
《Cracking the Coding Interview》——第16章:线程与锁——题目3相关推荐
- Cracking the coding interview
转自:http://hawstein.com/posts/ctci-solutions-contents.html Cracking the coding interview--问题与解答 March ...
- 经典算法题目:Cracking the coding interview 问题与解答
Cracking the coding interview--问题与解答 March 14, 2013 作者:Hawstein 出处: http://hawstein.com/posts/ctci-s ...
- [Free] Cracking the Coding Interview 6th Download
[Free] Cracking the Coding Interview 6th Download 推荐给有梯子的童鞋们! https://www.pdfdrive.com/cracking-the- ...
- 渣基础:比照Hawstein学Cracking the coding interview(1)
<C++ Primer 第五版>书实在是太长,太厚了.总是看了十几页就看累了,坚持不了多久,想了想还是别勉强自己,决定把它当工具书查看,或者积累足够的C++经验后再翻阅一遍. 目前的打算是 ...
- 《Cracking the Coding Interview》——第18章:难题——题目11
2014-04-29 04:30 题目:给定一个由'0'或者'1'构成的二维数组,找出一个四条边全部由'1'构成的正方形(矩形中间可以有'0'),使得矩形面积最大. 解法:用动态规划思想,记录二维数组 ...
- 《Cracking the Coding Interview》——第18章:难题——题目3
2014-04-29 01:02 题目:从m个整数里随机选出n个整数,要求等概率. 解法:和洗牌的算法类似,每次随机抽出一个数,抽n次即可.时间复杂度O(m * n),空间复杂度O(m). 代码: 1 ...
- 《Cracking the Coding Interview》——第11章:排序和搜索——题目7
2014-03-21 22:05 题目:给你N个盒子堆成一座塔,要求下面盒子的长和宽都要严格大于上面的.问最多能堆多少个盒子? 解法1:O(n^2)的动态规划解决.其实是最长递增子序列问题,所以也可以 ...
- 《Cracking the Coding Interview》——第9章:递归和动态规划——题目8
2014-03-20 04:04 题目:给你不限量的1分钱.5分钱.10分钱.25分钱硬币,凑成n分钱总共有多少种方法? 解法:理论上来说应该是有排列组合的公式解的,但推导起来太麻烦而且换个数据就又得 ...
- 《Cracking the Coding Interview》——第10章:可扩展性和存储空间限制——题目1
2014-04-24 00:38 题目:加入你要设计一个服务,供至多1000个client查询每日股票的最高.最低.开盘.收盘价,请描述你要如何设计这个服务. 解法:可以用SQL数据库组织,也可以用K ...
最新文章
- OC基础第四讲--字符串、数组、字典、集合的常用方法
- Windows Phone 7 网络编程之留言板应用
- win10系统 ubuntu子系统 进行ndk编译笔记
- 6种编写程序代码的方法
- 『数据中心』降低PUE值4种方法
- Visio studio 2019中opencv 4.1.1运行环境配置(亲测可用)
- Flask框架(flask中设置响应信息的方法,返回json数据的方法)
- Golang的值类型和引用类型的范围、存储区域、区别
- php post 获取xml,php 获取post的xml数据并解析示例
- Oracle ADG备库SYSAUX数据文件坏块恢复处理(ORA-00600,ORA-10567,ORA-10564......
- 我最喜欢的科目是计算机课英语,初一英语作:my favorite subject(我最喜欢的科目)要求写美术课(art),80词以上,拜托啦!...
- 使用IBM WID 建立SOA 之WID简介
- 忆2015,迎2016(致敬自己)
- C功底挑战Java菜鸟入门概念干货(三)
- Floating point exception
- 所有小米机型 解BT+刷Magisk并ROOT+躲避应用ROOT环境检查教程
- Intel Edison 基础开发之配置第一个小程序
- win 64 安装 sql server 2000、出现挂起 解决
- 淘宝首页幻灯片(二) 居中按钮源代码
- 选择排序(升序排列)