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

  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章:线程与锁——题目2

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

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

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

随机推荐

  1. Vim-命令合集

    命令历史 以:和/开头的命令都有历史纪录,可以首先键入:或/然后按上下箭头来选择某个历史命令. 启动vim 在命令行窗口中输入以下命令即可 vim 直接启动vim vim filename 打开vim ...

  2. sap.ui.require in SAP UI5 and require in nodejs

    UI5 例如我需要在controller的onShowHello里通过MessageToast弹一个消息显示在UI上, 我需要先定义我自己的controller,该controller extend自 ...

  3. HDU(1754),线段树,单点替换,区间最值

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1754 线段树模板题,update功能是单点替换,query是访问区间最大值. #include < ...

  4. jenkin+centos 7 环境搭建

    1.安装jenkins 首先安装好虚拟机和centos7操作系统  2.安装jdk 利用yum源来安装jdk(此方法不需要配置环境变量) 查看yum库中的java安装包 :yum -y list ja ...

  5. softmax sigmoid

    softmax和sigmoid实际上都是属于logistic regression,sigmoid是二分类的lr,拟合Bernoulli distribution(二项分布):0softmax是多分类 ...

  6. Spring boot 集成 Swagger

    添加依赖包 <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swa ...

  7. echarts学习笔记(部分angular及ant-design)

    1.在项目中修改ng-zorro组件默认样式的一些方法: 类名等 前加::ng-deep: 类名等 前加:root: 类名等 前加:host /deep/: 2.echarts横轴自定义时间粒度 两种 ...

  8. spfa判负权边

    spfa判负环 如果一个点在spfa中被入队了大于n次 那么,我们就能肯定,有负环出现. 因为一个点入队时,他肯定被更新了一次. 所以........ 如果不存在负权环.这个点最多被更新节点数次 我们 ...

  9. 【Java】数组知识回顾

    package another; import java.util.Arrays; import java.util.List; /** * 数组知识回顾 * @author ChristineBas ...

  10. Mantle--国外程序员最常用的iOS模型&字典转换框架

    Mantle简介 Mantle是iOS和Mac平台下基于Objective-C编写的一个简单高效的模型层框架. Mantle能做什么 Mantle可以轻松把JSON数据.字典(Dictionary)和 ...