《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 ...
随机推荐
- 关于Authorware的十二种使用技巧
Authorware是美国Macromedia公司(现已被adobe公司收购)开发的一种多媒体制作软件,它是一个图标导向式的多媒体开发工具.今天我们学习一下Authorware的十二种使用技巧,如果你 ...
- 3dsmax2014的下载、安装与注册激活教程详解
3dsmax2014的下载.安装与注册激活教程,虽然网上类似的教程文章不胜枚举,但大多比较粗枝大叶,没有详细的步骤,尤其对于电脑小白来说,更是不易参考,今天我就教大家如何注册破解3dsmax2014吧 ...
- oracle 11g expdp impdp详细使用方法
11G中有个新特性,当表无数据时,不分配segment,以节省空间 解决方法如下图: 二.oracle10g以后提供了expdp/impdp工具,同样可以解决此问题 1.导出expdp工具使用方法: ...
- HTML?这些还不懂咋办?
1.什么是空白折叠现象?为什么要空白折叠呢? 对于我们大多数人的习惯来讲,大都喜欢利用空格或者换行来调整文章的文字结构.这样往往可以使我们可以更轻松的阅读.但是,在HTML中却不允许我们这么做,这是为 ...
- 【转载】#335 - Accessing a Derived Class Using a Base Class Variable
You can use a variable whose type is a base class to reference instances of a derived class. However ...
- 【LOJ116】有源汇有上下界最大流(模板题)
点此看题面 大致题意: 给你每条边的流量上下界,让你先判断是否存在可行流.若存在,则输出最大流. 无源汇上下界可行流 在做此题之前,最好先去看看这道题目:[LOJ115]无源汇有上下界可行流. 大致思 ...
- Uva 11732 strcmp()函数
题目链接:https://vjudge.net/contest/158125#problem/A 题意: 系统中,strcmp函数是这样执行的,给定 n 个字符串,求两两比较时,strcmp函数要比较 ...
- 类似LIS+贪心(ZOJ1025)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=25 解题报告: #include <cstdio> #in ...
- 2018年Linux运维人员必会开源运维工具体系总结
操作系统:Centos,Ubuntu,Redhat,suse,Freebsd 网站服务:nginx,apache,lighttpd,php,tomcat,resin数据库:MySQL,MariaDB, ...
- C# do while语句
一.C# do while语句 do while语句是先执行一次循环体,然后再判断是否需要重复执行循环体的循环控制语句. 语法格式如下: do{ embedded-statement}while ...