《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 ...
随机推荐
- linux lnmp搭建
1.安装nginx: yum install gcc -y yum install -y pcre pcre-devel yum install -y zlib zlib-devel yum inst ...
- .net core 2.0下的容器注册方法
https://www.cnblogs.com/Wddpct/p/5764511.html 自带的容器注册方法真的很好用
- veritas.com常用资源汇总
NetBackup 8.1.2文档(合集) https://www.veritas.com/support/en_US/article.100044086 NetBackup产品组停止支持生命周期 ...
- [pytorch] Pytorch入门
Pytorch入门 简单容易上手,感觉比keras好理解多了,和mxnet很像(似乎mxnet有点借鉴pytorch),记一记. 直接从例子开始学,基础知识咱已经看了很多论文了... import t ...
- vector的几种初始化和遍历
随着C++11标准的出现,vector出现了新的初始化和遍历用法,但是vs2010和较高版本并没有能完全支持C++11标准,所以我就将它的所有的用法归纳了一下. vector的初始化 vector基本 ...
- ipython notebook的使用
刚开始使用python,用的是ipython notebook,感觉很好用. 写的程序主要是处理文件的,读写txt文件,生成xml文件,其中参考http://www.cnblogs.com/wangs ...
- vue 城市列表与字母表联动
实现两个联动 一是点击右侧字母的时候,城市列表出现相应首字母下的城市 二是鼠标在字母表上滑动的时候,城市列表实时跟着变化 一.点击字母出现相应的列表,给每个字母设置handleLetterClick事 ...
- 4、SpringBoot+Mybatis整合------一对多
开发工具:STS 代码下载链接:https://github.com/theIndoorTrain/SpringBoot_Mybatis/tree/c00b56dbd51a1e26ab9fd99020 ...
- webpack4基本配置
1.npm init 生成package.json文件 2.npm install webpack webpack-cil --save-dev 安装webpack和webpack-cli ...
- [vijos p1028] 魔族密码
描述 风之子刚走进他的考场,就……花花:当当当当~~偶是魅力女皇——花花!!^^(华丽出场,礼炮,鲜花)风之子:我呕……(杀死人的眼神)快说题目!否则……-_-###花花:……咦~~好冷~~我们现在要 ...