《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 ...
随机推荐
- Vim中修改列模式的文字为ABCD或1234的递增序号
vimrc里添加 vnoremap <F3> :<C-u>call <SID>AddCharOfCursor()<CR> "递增列模式下的字符 ...
- leetcode: 链表2
1. copy-list-with-random-pointer(拷贝一个带随机指针的链表) A linked list is given such that each node contains a ...
- 修改Windows下的Memcached下的服务端口
在命令模式下,可以使用 memcached -p 12000 去指定端口,可服务安装后,却总是只有 -d runservice 的参数. 通过修改注册表来达到这个修改端口的目的. 在 HKEY_LOC ...
- POJ-1509 Glass Beads---最小表示法模板
题目链接: https://vjudge.net/problem/POJ-1509 题目大意: 给你一个循环串,然后找到一个位置,使得从这个位置开始的整个串字典序最小. 解题思路: 最小表示法模板 注 ...
- Linux 初学者:移动文件
你学习了有关目录和访问目录的权限是如何工作的.你在这些文章中学习的大多数内容都可应用于文件 -- Paul Brown 在之前的该系列的部分中, 你学习了有关目录 和 访问目录 的权限 是如何工作的. ...
- 牛客国庆day 6 A
题目链接 : https://ac.nowcoder.com/acm/contest/206/A 这个题去年有幸去秦皇岛参加集训,见过这道题,当时特别菜还不会网络流,现在学了一点发现这个网络流还是比较 ...
- python将图像转化为矩阵
Image.fromarray(matrix).show()
- openstack kilo python cinderclient
➜ ~ pythonPython 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)] on ...
- 2.Spring Cloud初相识--------Eureka服务注册与消费
前言: 1.Eureka介绍: Spring Cloud Eureka,使用Netflix Eureka来实现服务注册与发现,它既包含了服务端组件,也包含了客户端组件,并且服务端与客户端均采用Java ...
- Session和cookic
session是无状态的方式,服务器存储机制,当用户第一次请求服务器,服务器会给客户分配一个标识id,客户端再次访问服务器,根据session id 去访问服务器数据库,返回信息,同时session ...