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的更多相关文章

  1. Cracking the coding interview 第一章问题及解答

    Cracking the coding interview 第一章问题及解答 不管是不是要挪地方,面试题具有很好的联系代码总用,参加新工作的半年里,做的大多是探索性的工作,反而代码写得少了,不高兴,最 ...

  2. 《Cracking the Coding Interview》读书笔记

    <Cracking the Coding Interview>是适合硅谷技术面试的一本面试指南,因为题目分类清晰,风格比较靠谱,所以广受推崇. 以下是我的读书笔记,基本都是每章的课后习题解 ...

  3. Cracking the coding interview

    写在开头 最近忙于论文的开题等工作,还有阿里的实习笔试,被虐的还行,说还行是因为自己的水平或者说是自己准备的还没有达到他们所需要人才的水平,所以就想找一本面试的书<Cracking the co ...

  4. 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 ...

  5. Cracking the coding interview目录及资料收集

    前言 <Cracking the coding interview>是一本被许多人极力推荐的程序员面试书籍, 详情可见:http://www.careercup.com/book. 第六版 ...

  6. Cracking the Coding Interview(Trees and Graphs)

    Cracking the Coding Interview(Trees and Graphs) 树和图的训练平时相对很少,还是要加强训练一些树和图的基础算法.自己对树节点的设计应该不是很合理,多多少少 ...

  7. 《Cracking the Coding Interview》——第16章:线程与锁——题目6

    2014-04-27 20:25 题目:关于java中标有synchronized的成员方法? 解法:这代表同一个对象实例的synchronized方法不能被多个线程同时调用.注意有这么多个地方都加粗 ...

  8. 《Cracking the Coding Interview》——第16章:线程与锁——题目5

    2014-04-27 20:16 题目:假设一个类Foo有三个公有的成员方法first().second().third().请用锁的方法来控制调用行为,使得他们的执行循序总是遵从first.seco ...

  9. 《Cracking the Coding Interview》——第16章:线程与锁——题目2

    2014-04-27 19:14 题目:如何测量上下文切换的时间? 解法:首先,上下文切换是什么,一搜就知道.对于这么一个极短的时间,要测量的话,可以通过放大N倍的方法.比如:有A和B两件事,并且经常 ...

  10. 《Cracking the Coding Interview》——第16章:线程与锁——题目1

    2014-04-27 19:09 题目:线程和进程有什么区别? 解法:理论题,操作系统教材上应该有很详细的解释.我回忆了一下,写了如下几点. 代码: // 16.1 What is the diffe ...

随机推荐

  1. 随便讲讲自己了解的ajax在JQ中的应用

    首先jQuery 库拥有完整的 Ajax 兼容套件.其中的函数和方法允许我们在不刷新浏览器的情况下从服务器加载数据. 定义和用法 ajax() 方法通过 HTTP 请求加载远程数据. 该方法是 jQu ...

  2. Leetcode 22. Generate Parentheses Restore IP Addresses (*) 131. Palindrome Partitioning

    backtracking and invariant during generating the parathese righjt > left  (open bracket and cloas ...

  3. lambda Expression的使用方法

    Expression<Func<your class, bool>> whereExp = f => true;//类似1=1,初始化条件 if (!string.IsN ...

  4. 【转】WebSocket 是什么原理?为什么可以实现持久连接?

    WebSocket是HTML5出的东西 也就是说HTTP协议没有变化 但HTTP是不支持持久连接的(长连接,循环连接的不算)或者说WebSocket干脆就不是基于HTTP来执行的.但是...说不通啊. ...

  5. 将命令的输出生成一个Web页面

    解决方法: ConvertTo-Html 命令: 生成一个HTML表格来代表命令的输出,为你提供的每个对象创建一行,在每行中,Powershell会创建代表对象属性的值. 实现效果:

  6. 14、SpringBoot------定制错误返回内容json格式

    开发工具:STS 前言: 在前后端分离的项目中,当前端向后端请求资源失败时,想知道具体的错误原因,给用户予以提示. 但是,在springboot中返回内容是固定的.并不适合我们前端进行分析. 所以,就 ...

  7. cncert阅读报告

    信息安全阅读报告 Problem 1: 国家计算机网络应急技术处理协调中心(简称“国家互联网应急中心”,英文缩写为“CNCERT”或“CNCERT/CC”)作为我国非政府层面网络安全应急体系核心技术协 ...

  8. c# .net 3.5 4.0 4.5 5.0 6.0各个版本新特性战略规划总结【转载】

    引用:http://blog.csdn.net/attilax/article/details/42014327 c# .net 3.5 4.0 各个版本新特性战略规划总结 1. ---------- ...

  9. Oracle_11g中解决被锁定的scott用户的方法

    在安装完Oracle10g和创建完oracle数据库之后,想用数据库自带的用户scott登录,看看连接是否成功. 问题: 在cmd命令中,用“sqlplus  scott/ tiger”登录时,老是提 ...

  10. tcl之控制流-break/continue