The Java™ Tutorials — Concurrency :Lock Objects 锁对象

The Java™ Tutorials — Concurrency :Lock Objects 锁对象

原文地址:https://docs.oracle.com/javase/tutorial/essential/concurrency/newlocks.html

关键点

  • Lock的使用

全文翻译

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.

同步代码块依赖于一种简单的可重入锁。这种锁简单易用,但是有很多限制。java.util.concurrent.locks包支持锁的更多的复杂用法。我们不会仔细讨论包中的细节,但是会专注于它的最基本的接口,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.

锁对象工作起来非常像同步代码块中使用的隐式锁。正如隐式锁那样,每次仅能有一个线程获取一个Lock对象。Lock对象同时支持wait/notify机制,虽然它们同时需要使用Condition对象。

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). The lockInterruptibly method backs out if another thread sends an interrupt before the lock is acquired.

Lock对象同隐式锁相比最大的优势在于它们从一个获取锁的尝试中撤出的能力。tryLock方法在锁不可达时会立刻撤出,或者在一个时间后撤出(如果指定的话)。如果在获得某锁前,另一线程发送了一个中断信号,那么lockInterruptilbly就会返回。

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:

让我使用Lock对象来解决我们在《活跃度》一文中遇到的死锁问题。Alphonse和Gaston现在可以在一个朋友将要鞠躬时,引起自己的留意。让我们模仿一下这个提升。现在要求我们的Friend对象在鞠躬前,必须获取双方的锁。下面就是为此提升模型设计的源代码。为了说明此方法的广泛性,我们假设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();
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值