2014-04-27 20:06

题目:设计一个类,只有在不产生死锁的时候才分配资源。

解法:不太清楚这个题是要分配何种资源,以何种形式?所以没能动手写个可运行的代码,只是闲扯了几句理论分析。

代码:

 // 16.4 Design a class which provides a lock only if no deadlock would take place.
. The class will maintain a list of all locks that would be requested by different processes.
private Vector<Lock> locks;
. The class will have a real-time record of which processes are using which locks. Mapping the process ID with a queue recording the locks it is using.
private HashMap<int, Queue<Lock> > lockOccupations;
. The class will perform deadlock detection before assigning a certain lock to a certain process.
private bool deadLockDetection(int processID, int lockID);
. The class will provide a public method to assign a lock to a process.
public bool getLock(int processID, int lockID); About dead lock detection:
Generally, dead lock detection is about detecting a loop in a directed graph. Using DFS will be a solution.

《Cracking the Coding Interview》——第16章:线程与锁——题目4的更多相关文章

  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. April 4 2017 Week 14 Tuesday

    Problems are not stop signs, they are guidelines. 问题不是休止符,而是引向标. It is ture during our explorations ...

  2. *5. Longest Palindromic Substring (dp) previous blogs are helpful

    Given a string s, find the longest palindromic substring in s. You may assume that the maximum lengt ...

  3. input,button制作按钮IE6,IE7点击时1px黑边框的解决方法

    按钮在IE6中点击时1px黑边框的最常见的解决方法 首先设置按钮为none,然后在按钮外面套一层来实现边框的效果,部分代码如下 .btnbox{ border:solid 1px red;} .btn ...

  4. 日常运维管理技巧一(查看负载 W)

    日常运维管理技巧一(查看负载 W) 今天针对Linux系统管理做一个专题的记录,以后会用的几率也是很大的,只要掌握必备的基础知识,做初级系统管理员是不成问题的. 作为一个运维工程师.系统管理员,如果对 ...

  5. LeetCode804. Unique Morse Code Words

    题目 国际摩尔斯密码定义一种标准编码方式,将每个字母对应于一个由一系列点和短线组成的字符串, 比如: "a" 对应 ".-", "b" 对应 ...

  6. BZOJ1050: [HAOI2006]旅行comf(并查集 最小生成树)

    Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4021  Solved: 2257[Submit][Status][Discuss] Descript ...

  7. html编写头部,mata的含义

    <meta name="viewport" content="width=device-width, initial-scale=1.0"> con ...

  8. mysql架构和历史

    存储引擎 查看: show table status like 'bigcourse'; 结果: +-----------+--------+---------+------------+------ ...

  9. django+xadmin在线教育平台(八)

    4-5 user modesl.py设计 循环引用: 设计app时每个app都有model   mark 如图:我们在user中定义usercourse记录用户学习的课程.会有两个外键:user和co ...

  10. 解决Cannot reinitialise DataTable问题 解决dataTables再次调用不能清空数据

    这里我们只需要多设置一个字段 “destroy" : true 即可 或者设置retrieve: true, 或者在加载datatable之前使用$("#example" ...