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. Windows 系统System帐号及权限

    今天碰到一同事,在那里删除注册表,死活都删除不掉,想起以前在学校的时候老是被莫名的被别人叫过去修电脑(开玩笑,真觉得那时候的我比现在牛B很多),什么删除不掉的东西没见过,然后小小的百度了一下很快就帮他 ...

  2. java——二叉树面试题

    import java.util.ArrayList; import java.util.Iterator; import java.util.LinkedList; import java.util ...

  3. 金庸的武侠世界和SAP的江湖

    2018年10月30日晚,成都地铁一号线,Jerry手机app上突然弹出来一条金庸去世的新闻. Jerry识字很早,小学一年级就开始蹭我父亲的<射雕英雄传>看了.小时候,我爸工作的车间里有 ...

  4. select_related()函数

    Django获取数据实体的时候,返回的对象一个实体或多个实体,也就是QuerySet,它是Django专有的东西,具体的理解,它是类似Python的字典的东西,但它并不实现字典的所有方法.今天讲解的是 ...

  5. 调用外部EXE文件

    实现效果: 知识运用: Process类的Start方法 实现代码: private void button1_Click(object sender, EventArgs e) { OpenFile ...

  6. 剑指offer39 平衡二叉树

    剑指上用了指针传递,这里用的引用传递 class Solution { public: bool IsBalanced_Solution(TreeNode* pRoot) { ; return IsB ...

  7. url 解析

    最近在做一个单页应用,使用AngularJS来处理一些页内路由(哈希#后的路由变化).自然会要解析URL中的参数.使用AngularJS自带的方法$location.search();可以自动将参数整 ...

  8. python基本使用时常见错误

    python基本使用时常见错误 字符编码错误 如果要学习计算机编程语言,首先就要搞懂字符编码,否则在以后的学习过程中,将会是一场噩梦.在一开始使用的时候,我就遇到了很多的关于字符编码的问题,做个简单的 ...

  9. typeid操作符

    typeid() operator返回type_info,返回值不可拷贝.不可赋值 // Illustrates the typeid operator. #include <iostream& ...

  10. 转 IOS7开发错误收集

    转自:http://blog.csdn.net/smallsky_keke/article/details/16117653 1. fatal error: file '/Applications/X ...