同步代碼依賴於簡單的可重入鎖,這種鎖易於使用,但有許多限制,java.util.concurrent.locks包支持更復雜的鎖定語法,咱們不會詳細檢查這個包,而是將重點放在其最基本的接口Lock上。html
Lock
對象的工做方式與同步代碼使用的隱式鎖定很是類似,與隱式鎖同樣,一次只有一個線程能夠擁有一個Lock
對象,Lock
對象還經過其關聯的Condition對象支持wait
/notify
機制。java
Lock
對象優於隱式鎖的最大優勢是它們可以退出獲取鎖的嘗試,若是鎖當即不可用或超時到期以前(若是指定),則tryLock
方法退出,若是另外一個線程在鎖被得到以前發送中斷,則lockInterruptibly
方法將退出。git
讓咱們使用Lock
對象來解決咱們在活性中看到的死鎖問題,Alphonse和Gaston訓練本身注意朋友何時要鞠躬。咱們經過要求咱們的Friend
對象必須在繼續執行bow
以前獲取兩個參與者的鎖來對此進行建模,如下是改進模型Safelock的源代碼。爲了演示這個語法的多功能性,咱們假設Alphonse和Gaston如此迷戀他們新發現的安全鞠躬能力,他們不能中止相互鞠躬:github
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(); } }