原子變量
在理解synchronized中有使用synchronized保證原子更新操作,但是使用synchronized成本太高了,需要先獲取鎖,最後還要釋放鎖,如果獲取不到鎖還需要等到。這些成本都是比較高的,對於這種情況,可以使用原子變量。
Java併發包中的基本原子變量類型有以下幾種:
- AtomicBoolean:原子Boolean類型,常用來在程序中表示一個標誌位
- AtomicInteger:原子Integer類型
- AtomicLong:原子Long類型,常用來在程序中生成唯一序列號
- AtomicReference:原子引用類型,用來以原子方式更新複雜類型
AtomicInteger
介紹
構造方法
// 給定一個初始值
public AtomicInteger(int initialValue)
// 初始值爲0
pubilc AtomicInteger()
它包含一些以原子方式實現組合操作的的方法
// 以原子方式獲取舊值並設置新值
public final int getAndSet(int newValue);
// 以原子方式獲取舊值並給當前值加1
public final int getAndIncrement();
// 以原子方式獲取舊值並給當前值減1
public final int getAndDecrement();
// 以原子方式獲取舊值並給當前值加delta
public final int getAndAdd(int delta);
// 以原子方式給當前值加1並獲取新值
public final int incrementAndGet();
// 以原子方式給當前值減1並獲取新值
public final int decrementAndGet();
這些方法的實現都依賴另一個public方法
public final boolean compareAndSet(int expect, int update);
compareAndSet方法,簡稱CAS。 該方法有兩個參數expect和udpate,如果當前值等於expect,則更新爲update,否則不更新,如果更新成功返回ture,否則返回false。這個操作都是原子性的。
AtomicInteger簡單使用
public class AtomicIntegerDemo {
private static AtomicInteger counter = new AtomicInteger();
static class Visitor extends Thread {
@Override
public void run() {
for (int i = 0; i < 1000; i++) {
counter.incrementAndGet();
}
}
}
public static void main(String[] args) throws InterruptedException {
Thread[] threads = new Thread[100];
for (int i = 0; i < threads.length; i++) {
threads[i] = new Visitor();
threads[i].start();
threads[i].join();
}
System.out.println(counter.get());
}
}
程序的輸出總是正確的,爲100000
AtomicInteger基本原理
AtomicInteger的大部分方法實現都類似,我們看一個方法:
public final int incrementAndGet() {
for(;;) {
int current = get();
int next = current + 1;
if(compareAndSet(current, next)) {
return next;
}
}
}
代碼是一個死循環,先獲取當前值current,計算期望的值next,然後調用CAS方法進行更新,如果更新沒有成功,說明value被別的線程改了,則再去取最新值並嘗試更新直到成功
compareAndSet是怎麼實現的呢?
public final boolean compareAndSet(int expect, int update) {
return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
}
unsafe的定義private static final Unsafe unsafe = Unsafe.getUnsafe();
位於sun.misc包下。
原理上,一般的計算機系統都在硬件層次上直接支持CAS指令
與synchronized的比較
與synchronized鎖相比,這種原子更新方式代表了一種不同的思維方式。
synchronized是悲觀的,它假定更新很可能衝突,所以先獲取鎖,再進行更新。
原子變量的更新是樂觀的,它假定衝突比較少,但是如果衝突了,就繼續嘗試更新。
synchronized是一種阻塞式算法,得不到鎖的時候就進入等待隊列,等待其他線程喚醒,有上下文切換的開銷。
原子變量的更新是非阻塞式的。
實現鎖
基於CAS,可以實現悲觀阻塞式算法
public class MyLock {
private AtomicInteger status = new AtomicInteger(0);
public void lock() {
while(!status.compareAndSet(0, 1)) {
Thread.yield();
}
}
public void unlock() {
status.compareAndSet(1, 0);
}
}
顯示鎖
接口Lock
public interface Lock {
// 獲取鎖
void lock();
// 獲取鎖,可以響應中斷
void lockInterrupteibly() throws InterruptedException;
// 嘗試獲取鎖,立即返回,不阻塞
boolean tryLock();
// 先嚐試獲取鎖,如果能成功則立即返回true,否則阻塞等待,但等待的最長時間由參數設置,在等待的同時可以響應中斷
boolean tryLock(long time, TimeUnit unit) throws InterruptedException;
// 釋放鎖
void unlock();
Condition newCondition();
}
相比於synchronized,顯示鎖支持以非阻塞方式獲取鎖,可以響應中斷、可以限時,使得它靈活很多 。
可重入鎖ReentrantLock
基本用法
Lock接口的主要實現類是ReentrantLock
public ReentrantLock()
public ReentrantLock(boolean fair)
參數fair表示是否保證公平,不指定的情況下,默認爲false,表示不保證公平。所謂公平是指,等待時間最長的線程有限獲得鎖。保證公平會影響性能,一般也不需要
實現原理
ReentrantLock的實現依賴類LockSupport。
LockSupport也位於java.util.concurrent.locks下,它主要的方法如下
// 使當前線程放棄CPU,進入WAITING狀態
public static void park()
public static void parkNanos(long nanos)
public static void parkUntil(long deadline)
public static void unpark(Thread thread)
park會使得當前線程放棄CPU,進行等待狀態i,直到有其他線程對它調用了unpaark,unpark使參數指定的線程恢復可運行狀態。
park不同於Thread.yield(), yield只是告訴操作系統可以先讓其他線程運行,但自己依然是可以運行狀態,而park會放棄調度資格,使線程進入WAITING狀態
AQS
利用CAS和LockSupport提供的基本方法就可以實現ReentrantLock了。
Java提供了一個抽象類AbstractQueuedSynchronizer,簡稱AQS,簡化併發工具的實現。
AQS封裝了一個狀態,給子類提供了查詢和設置狀態的方法
private volatile int state
protected final int getState()
protected final void setState(int newState)
protected final boolean compareAndSetState(int expect, int update)
用於實現鎖時,AQS可以保存鎖的當前持有線程,提供了方法進行查詢和設置
private transient Thread exclusiveOwnerThread;
protected final void setExclusiveOwnerThread(Thread t);
protected final Thread getExclusiveOwnerThread();
AQS內部維護了一個等待隊列,藉助CAS方法實現了無阻塞算法進行更新
ReentrantLock
ReentrantLock 內部使用了AQS,有三個內部類
abstract static class Sync extends AbstractQueuedSynchronizer
static final class NonfairSync extends Sync
static final class FairSync extends Sync
NonfairSync是fair爲false時使用的類,FairSync 是fair爲true時使用的類。在ReentrantLock中有一個Sync變量,方法的具體調用都是通過它來的
我們先看lock的實現
public void lock() {
sync.lock();
}
我們以默認的Nonfair類來進行分析
final void lock() {
// 如果當前未被鎖定,則立即獲得鎖
if (compareAndSetState(0, 1))
setExclusiveOwnerThread(Thread.currentThread());
else
// 獲得鎖
acquire(1);
}
ReentrantLock用state來表示鎖的狀態
如果當前未被鎖定,則立即獲得鎖,否則通過acquire(1)
獲得鎖。
acquire(1)
是AQS中的方法
// AbstractQueuedSynchronizer.java
public final void acquire(int arg) {
if (!tryAcquire(arg) &&
acquireQueued(addWaiter(Node.EXCLUSIVE), arg))
selfInterrupt();
}
調用tryAcquire
獲取鎖,tryAcquire必須被子類重寫
NonfairSync實現如下:
// NonfairSync
protected final boolean tryAcquire(int acquires) {
return nonfairTryAcquire(acquires);
}
nonfairTryAcquire是sync中實現:
// Sync
final boolean nonfairTryAcquire(int acquires) {
final Thread current = Thread.currentThread();
int c = getState();
// 如果未被鎖定,則使用CAS進行鎖定
if (c == 0) {
if (compareAndSetState(0, acquires)) {
setExclusiveOwnerThread(current);
return true;
}
}
// 如果已經被當前線程鎖定,則增加鎖定次數
else if (current == getExclusiveOwnerThread()) {
int nextc = c + acquires;
if (nextc < 0) // overflow
throw new Error("Maximum lock count exceeded");
setState(nextc);
return true;
}
return false;
}
如果tryAcquire
返回false,則AQS會調用:
// addWaiter會新建一個節點,代表當前線程,然後加入內部的等待隊列中
acquireQueued(addWaiter(Node.EXCLUSIVE), arg);
final boolean acquireQueued(final Node node, int arg) {
boolean failed = true;
try {
boolean interrupted = false;
for (;;) {
final Node p = node.predecessor();
// 檢查當前節點是否是第一個等待的節點
// 如果是,再判斷是否能獲取到鎖
if (p == head && tryAcquire(arg)) {
setHead(node);
p.next = null; // help GC
failed = false;
return interrupted;
}
if (shouldParkAfterFailedAcquire(p, node) &&
// 調用LockSupport.park放棄CPU,返回中斷標誌
parkAndCheckInterrupt())
interrupted = true;
}
} finally {
if (failed)
cancelAcquire(node);
}
}
小結lock方法的基本過程:
1. 能獲取鎖就立即獲得,否則加入等待隊列
2. 被喚醒後檢查自己是否是第一個等待的線程,如果是且能獲得鎖則返回。否則繼續等待
3. 在這個過程中,如果發生了中斷,lock會記錄中斷標誌位,但不會提前返回或拋出異常
unlock的實現主要是修改狀態釋放鎖。
不過FairSync和NonfairSync的區別是:在獲取鎖時,FairSync多了一個檢測,只有不存在其他等待時間更長的線程,它纔會獲取鎖。
保證公平整體性能比較低,低的原因不是這裏多了一個檢查,而是會讓活躍線程得不到鎖,進入等待狀態,引起頻繁上下文切換,降低了整體的效率
顯示條件
鎖用於解決競態條件問題,條件是線程間的協作機制。
wait/notify與synchronized配合使用, 顯示條件與顯示鎖配合使用。
Condition 表示條件變量,是一個接口
public interface Condition {
void await() throws InterruptedException;
void awaitUninterruptibly();
long awaitNanos(long nanosTimeout) throws InterruptedException;
boolean await(long time, TimeUnit unit) throws InterruptedException;
boolean awaitUntil(Date deadline) throws InterruptedException;
void signal();
void signalAll();
}
await對應於Object的wait,signal對應於notify,signalAll對應於notifyAll
調用await方法前需要先獲取鎖。await在進入等待隊列後,會釋放鎖,釋放CPU,當其他線程將它喚醒後,或等待超時後,或發生中斷異常後,它都需要重新獲取鎖,獲取鎖後,纔會從await方法中退出