《Cracking the Coding Interview》——第16章:线程与锁——题目5
2014-04-27 20:16
题目:假设一个类Foo有三个公有的成员方法first()、second()、third()。请用锁的方法来控制调用行为,使得他们的执行循序总是遵从first、second、third的顺序。
解法:你应该想到了用lock的方法类阻塞,不过这里面有个概念问题使得直接用ReentrantLock不能通过编译(对于一个锁对象,不同在A线程中锁定,又在B线程中解锁,不允许这样的归属关系),可以用Semaphore来达到相同的目的。请看下面的代码。
代码:
// 16.5 There're three methods in a class FooBar, how would you make sure that they're executed in a fixed order, in whichever order they're called?
public class FirstRun implements Runnable {
private FooBar fooBar; public FirstRun(FooBar fooBar) {
// TODO Auto-generated constructor stub
this.fooBar = fooBar;
} @Override
public void run() {
// TODO Auto-generated method stub
fooBar.first();
}
} // -----------------------------------------------------------------------------
public class SecondRun implements Runnable {
private FooBar fooBar; public SecondRun(FooBar fooBar) {
// TODO Auto-generated constructor stub
this.fooBar = fooBar;
} @Override
public void run() {
// TODO Auto-generated method stub
fooBar.second();
}
} // -----------------------------------------------------------------------------
public class ThirdRun implements Runnable {
private FooBar fooBar; public ThirdRun(FooBar fooBar) {
// TODO Auto-generated constructor stub
this.fooBar = fooBar;
} @Override
public void run() {
// TODO Auto-generated method stub
fooBar.third();
}
} // -----------------------------------------------------------------------------
import java.util.concurrent.Semaphore; public class FooBar {
private Semaphore sem1;
private Semaphore sem2;
private Semaphore sem3; public FooBar() {
// TODO Auto-generated constructor stub
sem1 = new Semaphore(1);
sem2 = new Semaphore(1);
sem3 = new Semaphore(1); try {
sem1.acquire();
sem2.acquire();
sem3.acquire();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} public void first() {
System.out.println("first"); sem1.release();
} public void second() {
try {
sem1.acquire();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sem1.release();
System.out.println("second");
sem2.release();
} public void third() {
try {
sem2.acquire();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
sem2.release();
System.out.println("third");
sem3.release();
} public static void main(String[] args) {
FooBar fooBar = new FooBar();
Thread t1 = new Thread(new FirstRun(fooBar));
Thread t2 = new Thread(new SecondRun(fooBar));
Thread t3 = new Thread(new ThirdRun(fooBar)); t3.start();
t1.start();
t2.start();
}
}
《Cracking the Coding Interview》——第16章:线程与锁——题目5的更多相关文章
- 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章:线程与锁——题目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 ...
随机推荐
- ES6相关特性(let & const)
[ecma-262/8.0]http://www.ecma-international.org/ecma-262/8.0/index.html 1.Let & const let 的三个特性: ...
- PHP : url中出现乱码问题
例子: 在html中,将数据传到url中 当我点击“提交回复”后,跳转页面中将显示: 我们获取这个参数: 但是由于传过来的参数是中文,url会进行自动的解析成二进制的代码,那我们后台接受到的数据是解析 ...
- PHP: 打印post数据,返回长度不为1但内容为空的问题
问题: 首先,html长这样: 我们可以看到textarea标签的name值为cten,那么我们进行查看Post是否能够正常获取到数据: 后台进行获取: 结果:我们可以正常获取到数据 接下来,我们进行 ...
- 电路设计软件 电路模拟软件 sPlan , LTspice 等
电路设计/PCB绘制 立创EDA https://lceda.cn/ sPlan http://www.electronic-software-shop.com/splan-70.html?langu ...
- Objectbox Box的getAll() 函数返回emptylist() 未判断导致崩溃
最近使用了Objectbox作为新项目的数据库后台,Greendao开发团队新力作,但是Objectbox算是比较新的一个东西,现在资料也不多. 今天跟大家分享一个关于Box类的getAll()函数的 ...
- hdu-1247 Hat’s Words---字典树模板
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1247 题目大意: 给出一些单词,以EOF结束,看其中哪一个单词可以由其他两个单词组成,将其输出 解题 ...
- Codeforces 758D Ability To Convert(区间DP)
题目链接:http://codeforces.com/problemset/problem/758/D 题意:一个n进制下的数k,其中k不会用字母,如果有A就用10代替了.求k这个数对应的,在10进制 ...
- 广搜,智能拼图(ZOJ1079)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=79 解题报告: 思路简单,写法太难. #include <std ...
- 模拟网页的浏览Stack(POJ1028)
题目链接:http://poj.org/problem?id=1028 注意: 1.用两个栈来模拟,一个用来存可以返回的,一个用来存可以前进的. 2.visit方法,就要将可以前进的栈清空. 3.ba ...
- 将matlab处理结果保存为图像文件
imwrite(testIm, 'Data/Test/testIm.bmp', 'BMP');