到目前為止,本課程一直都專注于底層次的API——從非常早開始就一直是JAVA平臺的組成部分。這些API對于解決一些基本問題是非常足夠的,但是為了應對更高級的任務,就需要更高層次的組成模塊。這對于充分利用了目前的多核和多CPU的大規模的并發應用來說就更是如此。
在這個章節,我們會探究一些在JAVA5.0才引入的高層次的并發特征。其中大部分的特征都在java.util.concurrent包中實現。JAVA集合框架中也有新的并發數據類型引入。
- Lock對象支持鎖動作,這可以簡化很多并發的開發。
- Executors定義了一個可以發起和管理線程的高層次API。由java.util.concurrent包提供的Executor實現提供了適合大型應用的線程池管理。
- 原子變量Atomic可以最小化同步的開銷,以及幫助防止內存不一致性錯誤。
- ThreadLocalRandom類提供了使用多線程有效生成偽隨機數的方法。
鎖對象
同步代碼依賴一種非常簡單的可重入鎖。這種重入鎖使用很方便,但是有很多局限性。java.util.concurrent包支持更多復雜的鎖操作。我們不會非常仔細地考察這個包,但是我們會關注它最底層的接口,Lock。
Lock對象的工作方式和同步方法使用的不確切鎖很像。和不確切鎖一樣,同時只有一個線程能擁有鎖。Lock對象也支持wait/notify機制,通過與他們相關聯的Condition對象。
鎖對象與不確切鎖相比最大的優勢在于它們能夠從獲取鎖的嘗試中返回。tryLock方法會返回如果不能立刻、或在某段時間內獲得鎖。lockInterruptibly方法會返回如果另一個線程在獲取鎖之前發送了一個中斷。
讓我們使用Lock對象來解決我們在活性章節看到的死鎖問題。Alphonse和Gaston已經訓練他們自己能夠發現對方準備要鞠躬。我們規定如果想要鞠躬,Friend對象必須要獲取兩者的鎖。
下面是安全鎖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();
}
}