Lock Objects

Synchronized code relies on a simple kind of reentrant lock. This kind of lock is easy to use, but has many limitations. More sophisticated locking idioms are supported by the java.util.concurrent.locks package. We won't examine this package in detail, but instead will focus on its most basic interface, Lock.

Lock objects work very much like the implicit locks used by synchronized code. As with implicit locks, only one thread can own a Lock object at a time. Lock objects also support a wait/notify mechanism, through their associated Condition objects.

The biggest advantage of Lock objects over implicit locks is their ability to back out of an attempt to acquire a lock. The tryLock method backs out if the lock is not available immediately or before a timeout expires (if specified). ThelockInterruptibly method backs out if another thread sends an interrupt before the lock is acquired.

Let's use Lock objects to solve the deadlock problem we saw in Liveness. Alphonse and Gaston have trained themselves to notice when a friend is about to bow. We model this improvement by requiring that our Friend objects must acquire locks for both participants before proceeding with the bow. Here is the source code for the improved model, Safelock. To demonstrate the versatility of this idiom, we assume that Alphonse and Gaston are so infatuated with their newfound ability to bow safely that they can't stop bowing to each other:

import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.Random; public class Safelock {
static class Friend {
private final String name;
private final Lock lock = new ReentrantLock(); public Friend(String name) {
this.name = name;
} public String getName() {
return this.name;
} public boolean impendingBow(Friend bower) {
Boolean myLock = false;
Boolean yourLock = false;
try {
myLock = lock.tryLock();
yourLock = bower.lock.tryLock();
} finally {
if (! (myLock && yourLock)) {
if (myLock) {
lock.unlock();
}
if (yourLock) {
bower.lock.unlock();
}
}
}
return myLock && yourLock;
} public void bow(Friend bower) {
if (impendingBow(bower)) {
try {
System.out.format("%s: %s has"
+ " bowed to me!%n",
this.name, bower.getName());
bower.bowBack(this);
} finally {
lock.unlock();
bower.lock.unlock();
}
} else {
System.out.format("%s: %s started"
+ " to bow to me, but saw that"
+ " I was already bowing to"
+ " him.%n",
this.name, bower.getName());
}
} public void bowBack(Friend bower) {
System.out.format("%s: %s has" +
" bowed back to me!%n",
this.name, bower.getName());
}
} static class BowLoop implements Runnable {
private Friend bower;
private Friend bowee; public BowLoop(Friend bower, Friend bowee) {
this.bower = bower;
this.bowee = bowee;
} public void run() {
Random random = new Random();
for (;;) {
try {
Thread.sleep(random.nextInt(10));
} catch (InterruptedException e) {}
bowee.bow(bower);
}
}
} public static void main(String[] args) {
final Friend alphonse =
new Friend("Alphonse");
final Friend gaston =
new Friend("Gaston");
new Thread(new BowLoop(alphonse, gaston)).start();
new Thread(new BowLoop(gaston, alphonse)).start();
}
}

译文:
锁对象
  同步代码依赖于一种简单的重入锁。这种锁很容易使用,但是却有很多限制。java.util.concurrent.locks包中支持许多更复杂的锁的规则。我们不会详细的校验
这个包中的东西,而是把重点放在其最基本的接口,锁上。
  锁对象的工作原理和通过同步方法严格实现的锁一样。同严格实现的锁一样,一个线程一次只能拥有一个锁对象。通过关联Condition对象,锁对象也支持wait/notify机制。
  锁对象比严格的锁最大的优势是他们能够从获得一个锁返回。tryLock方法从一个没有立即使用的锁或者超过期望时间(如果)的锁。lockInterruptibly方法会使若果另一个线程向这个锁发送一个中断请求返回。
  让我们用锁对象来解决在Liveness中出现的死锁问题。Alphone和Gaston被训练成为各自都能注意到一个朋友正在鞠躬。我们要求我们的朋友必须获得锁让后再继续鞠躬的这种改进模型。这里是这种模型的改进模型,safelock。为了证明这些规则的功能性,我们假设Alphonse和Gaston都非常痴迷于他们的新发现安全的鞠躬,他们不能互相阻止对方的鞠躬。

 import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
import java.util.Random; public class Safelock {
static class Friend {
private final String name;
private final Lock lock = new ReentrantLock(); public Friend(String name) {
this.name = name;
} public String getName() {
return this.name;
} public boolean impendingBow(Friend bower) {
Boolean myLock = false;
Boolean yourLock = false;
try {
myLock = lock.tryLock();
yourLock = bower.lock.tryLock();
} finally {
if (! (myLock && yourLock)) {
if (myLock) {
lock.unlock();
}
if (yourLock) {
bower.lock.unlock();
}
}
}
return myLock && yourLock;
} public void bow(Friend bower) {
if (impendingBow(bower)) {
try {
System.out.format("%s: %s has"
+ " bowed to me!%n",
this.name, bower.getName());
bower.bowBack(this);
} finally {
lock.unlock();
bower.lock.unlock();
}
} else {
System.out.format("%s: %s started"
+ " to bow to me, but saw that"
+ " I was already bowing to"
+ " him.%n",
this.name, bower.getName());
}
} public void bowBack(Friend bower) {
System.out.format("%s: %s has" +
" bowed back to me!%n",
this.name, bower.getName());
}
} static class BowLoop implements Runnable {
private Friend bower;
private Friend bowee; public BowLoop(Friend bower, Friend bowee) {
this.bower = bower;
this.bowee = bowee;
} public void run() {
Random random = new Random();
for (;;) {
try {
Thread.sleep(random.nextInt(10));
} catch (InterruptedException e) {}
bowee.bow(bower);
}
}
} public static void main(String[] args) {
final Friend alphonse =
new Friend("Alphonse");
final Friend gaston =
new Friend("Gaston");
new Thread(new BowLoop(alphonse, gaston)).start();
new Thread(new BowLoop(gaston, alphonse)).start();
}
}

养养眼^_^

【翻译十八】java-并发之锁对象的更多相关文章

  1. JAVA之旅(十八)——基本数据类型的对象包装类,集合框架,数据结构,Collection,ArrayList,迭代器Iterator,List的使用

    JAVA之旅(十八)--基本数据类型的对象包装类,集合框架,数据结构,Collection,ArrayList,迭代器Iterator,List的使用 JAVA把完事万物都定义为对象,而我们想使用数据 ...

  2. Java并发之锁升级:无锁->偏向锁->轻量级锁->重量级锁

    Java并发之锁升级:无锁->偏向锁->轻量级锁->重量级锁 对象头markword 在lock_bits为01的大前提下,只有当是否偏向锁位值为1的时候,才表明当前对象处于偏向锁定 ...

  3. 【转】设计模式 ( 十八 ) 策略模式Strategy(对象行为型)

    设计模式 ( 十八 ) 策略模式Strategy(对象行为型) 1.概述 在软件开发中也常常遇到类似的情况,实现某一个功能有多种算法或者策略,我们可以根据环境或者条件的不同选择不同的算法或者策略来完成 ...

  4. 设计模式 ( 十八 ) 策略模式Strategy(对象行为型)

    设计模式 ( 十八 ) 策略模式Strategy(对象行为型) 1.概述 在软件开发中也经常遇到类似的情况,实现某一个功能有多种算法或者策略,我们能够依据环境或者条件的不同选择不同的算法或者策略来完毕 ...

  5. java并发之固定对象与实例

    java并发之固定对象与实例 Immutable Objects An object is considered immutable if its state cannot change after ...

  6. 转 Java并发之锁的升级

    说明:本文大部分内容来自<并发编程的艺术>,再加上自己网络整理和理解 以下内容来自<java并发编程的艺术>作者:方鹏飞 魏鹏 程晓明 在多线程并发编程中synchronize ...

  7. Java并发编程(十)-- Java中的锁

    在学习或者使用Java的过程中进程会遇到各种各样的锁的概念:公平锁.非公平锁.自旋锁.可重入锁.偏向锁.轻量级锁.重量级锁.读写锁.互斥锁.死锁.活锁等,本文将简概的介绍一下各种锁. 公平锁和非公平锁 ...

  8. JAVA并发之锁获取步骤及锁优化

    在另外的两篇文章中先后介绍了轻量级同步关键字volatile和重量级锁关键字synchronized,这两个关键字是Java语言中进行线程同步的基本方式(当然还有ReentrenLock等显式锁方式) ...

  9. 【翻译十五】-java并发之固定对象与实例

    Immutable Objects An object is considered immutable if its state cannot change after it is construct ...

随机推荐

  1. Java常见的几种排序算法-插入、选择、冒泡、快排、堆排等

    本文就是介绍一些常见的排序算法.排序是一个非常常见的应用场景,很多时候,我们需要根据自己需要排序的数据类型,来自定义排序算法,但是,在这里,我们只介绍这些基础排序算法,包括:插入排序.选择排序.冒泡排 ...

  2. NSOperation

    自定义operation 相比GCD,可以中断任务,也可使用 addDependency,对要执行的任务进行排序.. // // CustomOperation.h // Test // // Cre ...

  3. UICollectionView + AFNetWorking 异步加载,局部刷新.

    最近在做的项目需要cell里面的数据需要和后台实时交互.而collectionview reload会整体刷新. //m层 发送通知 [[NSNotificationCenter defaultCen ...

  4. 1.6---旋转二维数组,旋转图像像素,旋转矩阵,90度(CC150)

    import java.util.*; public class Transform { public int[][] transformImage(int[][] matrix, int n) { ...

  5. 应用HTK搭建语音拨号系统4: 识别器评估

    选自:http://maotong.blog.hexun.com/6261890_d.html 苏统华 哈尔滨工业大学人工智能研究室 2006年10月30日 声明:版权所有,转载请注明作者和来源 该系 ...

  6. phpcms后台进入地址(包含No permission resources错误)

    安装phpcms后却不知道怎么进入后台,实际上输入如下地址即可进入后台登陆界面: http://你的域名/admin.php 如果出现No permission resources.错误,可能是之前修 ...

  7. php之aop实践

    aop简介 AOP为Aspect Oriented Programming的缩写,意为:面向切面编程(也叫面向方面),可以通过预编译方式和运行期动态代理实现在不修改源代码的情况下给程序动态统一添加功能 ...

  8. poj 2153

    题意:题目还是很简单的,就是求Li Ming 在班上的排名,而且成绩是相加的. 思路:用map就行.不然好像用qsort+二分也可以,不过我在那里碰到了一些状况,然后就没用这种方法了,简单的map就可 ...

  9. Core Data 多线程时多个context使用

    原文地址:http://blog.csdn.net/willmomo/article/details/19759413 写的非常好!我这里截取一点重要的,备份,请去原地址阅读! 本人去年10月份才接触 ...

  10. 5.js模式-职责链模式

    1. 职责链模式 将对象连成一条链,并沿着这条链传递请求,直到有一个对象处理它为止. var chain = function(fn){ this.fn = fn; this.successor = ...