《Cracking the Coding Interview》——第16章:线程与锁——题目3
2014-04-27 19:26
题目:哲学家吃饭问题,死锁问题经典模型(专门用来黑哲学家的?)。
解法:死锁四条件:1. 资源互斥。2. 请求保持。3. 非抢占。4. 循环等待。所以,某砖家拿起一只筷子后如果发现没有另一只了,就必须把手里这只筷子放下,这应该是通过破坏“请求保持”原则来防止死锁产生,请求资源失败时,连自己的资源也进一步释放,然后在下一轮里继续请求,直到成功执行。
代码:
// This is the class for chopsticks.
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock; public class Chopstick {
private Lock lock; public Chopstick() {
lock = new ReentrantLock();
} public boolean pickUp() {
return lock.tryLock();
} public void putDown() {
lock.unlock();
}
} //------------------------------------I'm a delimiter------------------------------------
// This is the class for philosophers.
import java.util.Vector; public class Philosopher extends Thread {
private Chopstick left;
private Chopstick right;
private int id;
int appetite; final int FULL_APPETITE = 10; public Philosopher(Chopstick left, Chopstick right, int id) {
// TODO Auto-generated constructor stub
appetite = 0;
this.left = left;
this.right = right;
this.id = id;
} private boolean pickUp() {
if (!left.pickUp()) {
return false;
}
if (!right.pickUp()) {
left.putDown();
return false;
}
return true;
} private void putDown() {
left.putDown();
right.putDown();
} public boolean eat() {
while (appetite < FULL_APPETITE) {
if (!pickUp()) {
return false;
}
System.out.println(id + ":chew~");
++appetite;
putDown();
}
return appetite == FULL_APPETITE;
} @Override
public void run() {
// TODO Auto-generated method stub
super.run();
while (!eat()) {
// Not full yet.
}
} public static void main(String[] args) {
final int n = 6;
Vector<Chopstick> chopsticks = new Vector<Chopstick>();
Vector<Philosopher> philosophers = new Vector<Philosopher>(); for (int i = 0; i < n; ++i) {
chopsticks.add(new Chopstick());
}
for (int i = 0; i < n; ++i) {
philosophers.add(new Philosopher(chopsticks.elementAt(i),
chopsticks.elementAt((i + 1) % n), i + 1));
} for (int i = 0; i < n; ++i) {
philosophers.elementAt(i).start();
}
}
}
《Cracking the Coding Interview》——第16章:线程与锁——题目3的更多相关文章
- Cracking the coding interview 第一章问题及解答
Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...
- 《Cracking the Coding Interview》读书笔记
<Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...
- Cracking the coding interview
写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...
- Cracking the Coding Interview(Stacks and Queues)
Cracking the Coding Interview(Stacks and Queues) 1.Describe how you could use a single array to impl ...
- Cracking the coding interview目录及资料收集
前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...
- Cracking the Coding Interview(Trees and Graphs)
Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...
- 《Cracking the Coding Interview》——第16章:线程与锁——题目6
2014-04-27 20:25 题目:关于java中标有synchronized的成员方法? 解法:这代表同一个对象实例的synchronized方法不能被多个线程同时调用.注意有这么多个地方都加粗 ...
- 《Cracking the Coding Interview》——第16章:线程与锁——题目5
2014-04-27 20:16 题目:假设一个类Foo有三个公有的成员方法first().second().third().请用锁的方法来控制调用行为,使得他们的执行循序总是遵从first.seco ...
- 《Cracking the Coding Interview》——第16章:线程与锁——题目2
2014-04-27 19:14 题目:如何测量上下文切换的时间? 解法:首先,上下文切换是什么,一搜就知道.对于这么一个极短的时间,要测量的话,可以通过放大N倍的方法.比如:有A和B两件事,并且经常 ...
- 《Cracking the Coding Interview》——第16章:线程与锁——题目1
2014-04-27 19:09 题目:线程和进程有什么区别? 解法:理论题,操作系统教材上应该有很详细的解释.我回忆了一下,写了如下几点. 代码: // 16.1 What is the diffe ...
随机推荐
- TP5.1 配置的获取与设置
我们现在学习对配置文件的获取(Config::get)与设置(Config::set) 我们将学会: (1)获取到一级配置文件 (2)获取到二级配置文件 (3)设置二级配置文件 1.获取一级配置文件 ...
- AFNetworking 初探
AFNetworking 初探 繼ASIHTTPRequest發佈不再維護的訊息之後,如果我們不使用CDN(雲端伺服器),AFNetworking 會是一套不錯的選擇.下載網址:https://git ...
- 制作URL以GET方式提交的简单加密程序
首先我们用到的是 DESCryptoServiceProvider 类 对此微软给出的解释是 定义访问数据加密标准 (DES) 算法的加密服务提供程序 (CSP) 版本的包装对象.无法继承此类. 接下 ...
- ACM Arabella Collegiate Programming Contest 2015 H. Capital City 边连通分量
题目链接:http://codeforces.com/gym/100676/attachments 题意: 有 n 个点,m 条边,图中,边强连通分量之间可以直达,即距离为 0 ,找一个点当做首都,其 ...
- openstack kilo 命令行
把下面内容放到.bashrc中,或者直接执行也行. export OS_USERNAME=adminexport OS_PASSWORD=admin #根据实际密码来设 ...
- cudaMemcpy2D介绍
cudaMemcpy2D( d_A, // 目的指针 d_pitch, // 目的pitch bmp1, // 源指针 sizeof(int)*2, // 源数据pitch sizeof(int)*2 ...
- 旧文备份:对象字典0x1005和0x1006的理解
SYNC不一定由主站产生,因此,产生SYNC的节点,0x1005对象的值一般是0x40000080,第30位为1表示本节点产生 SYNC,而本节点的0x1006对象就是产生同步周期值了;而接收SYNC ...
- 第41章 RS-485通讯实验—零死角玩转STM32-F429系列
第41章 RS-485通讯实验 全套200集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/fir ...
- Layer弹出层关闭后刷新父页面
API地址:http://layer.layui.com/api.html#end 调用END回调方法: end - 层销毁后触发的回调 类型:Function,默认:null 无论是确认还是取消,只 ...
- vue-wechat-title
html中的title安装:npm install vue-wechat-title --save1.在mian.js中//网页titleimport VueTitle from 'vue-wecha ...