在上一節中,
我們已經了解了Java多線程編程中常用的關鍵字synchronized,以及與之相關的對象鎖機制。這一節中,讓
我們一起來認識JDK 5中新引入的并發框架中的鎖機制。
我想很多購買了《Java程序員面試寶典》之類圖書的朋友一定對下面
這個面試題感到非常熟悉:
問:請對比synchronized與java.util.concurrent.locks.Lock
的異同。
答案:主要相同點:Lock能完成synchronized所實現的所有功能
主要不同點:Lock有比synchronized更精確的線程語義和更好的性能。synchronized會自動釋放
鎖,而Lock一定要求程序員手工釋放,并且必須在finally從句中釋放。
恩,讓我們先鄙視一下應試教育。
言歸正傳,我們先來看一個多線程程序。它使用多個線程對一個Student對象進行訪問,改變其中的變量值。
我們首先用傳統的synchronized 機制來實現它:
運行結果:
顯然,在這個程序中,由于兩段synchronized塊使用了同樣的對象做為對象鎖,所以JVM優先使剛剛釋放該鎖的線程重新獲得該
鎖。這樣,每個線程執行的時間是10秒鐘,并且要徹底把兩個同步塊的動作執行完畢,才能釋放對象鎖。這樣,加起來一共是
30秒。
轉載注明出處:http://x- spirit.javaeye.com/、http: //www.aygfsteel.com/zhangwei217245/
我想一定有人會說:如果兩段synchronized塊采用兩個不同的對象鎖,就可以提高程序的并發性,并且,這
兩個對象鎖應該選擇那些被所有線程所共享的對象。
那么好。我們把第二個同步塊中的對象鎖改為student(此處略去代碼,讀
者自己修改),程序運行結果為:
從 修改后的運行結果來看,顯然,由于同步塊的對象鎖不同了,三個線程的執行順序也發生了變化。在一個線程釋放第一個同步塊的同步鎖之
后,第二個線程就可以進入第一個同步塊,而此時,第一個線程可以繼續執行第二個同步塊。這樣,整個執行過程中,有10秒鐘
的時間是兩個線程同時工作的。另外十秒鐘分別是第一個線程執行第一個同步塊的動作和最后一個線程執行第二個同步塊的動作。相比較第一
個例程,整個程序的運行時間節省了1/3。細心的讀者不難總結出優化前后的執行時間比例公式:(n+1)/2n,其中n為
線程數。如果線程數趨近于正無窮,則程序執行效率的提高會接近50%。而如果一個線程的執行階段被分割成m個
synchronized塊,并且每個同步塊使用不同的對象鎖,而同步塊的執行時間恒定,則執行時間比例公式可以寫作:((m-
1)n+1)/mn那么當m趨于無窮大時,線程數n趨近于無窮大,則程序執行效率的提升幾乎可以達到100%。(顯然,我
們不能按照理想情況下的數學推導來給BOSS發報告,不過通過這樣的數學推導,至少我們看到了提高多線程程序并發性的一種方案,而
這種方案至少具備數學上的可行性理論支持。)
轉載注明出處:http://x- spirit.javaeye.com/、http: //www.aygfsteel.com/zhangwei217245/
可見,使用不同的對象鎖,在不同的同步塊中完成任務,可以使性能大大提升。
很多人看到這不禁要問:這和新的Lock框 架有什么關系?
別著急。我們這就來看一看。
synchronized塊的確不錯,但是他有一些功能性的限制:
1. 它無法中斷一個正在等候獲得鎖的線程,也無法通過投票得到鎖,如果不想等下去,也就沒法得到鎖。
2.synchronized
塊對于鎖的獲得和釋放是在相同的堆棧幀中進行的。多數情況下,這沒問題(而且與異常處理交互得很好),但是,確實存在一些更適合使用
非塊結構鎖定的情況。
轉載注明出處:http://x- spirit.javaeye.com/、http: //www.aygfsteel.com/zhangwei217245/
java.util.concurrent.lock 中的 Lock 框架是鎖定的一個抽象,它允許把鎖定的實現作為 Java 類,而不是作為語言的特性來實現。這就為 Lock 的多種實現留下了空間,各種實現可能有不同的調度算法、性能特性或者鎖定語義。
JDK 官方文檔中提到:
ReentrantLock是“一個可重入的互斥鎖 Lock,它具有與使用 synchronized 方法和語句所訪問的隱式監視器鎖相同的一些基本行為和語義,但功能更強大。
ReentrantLock 將由最近成功獲得鎖,并且還沒有釋放該鎖的線程所擁有。當鎖沒有被另一個線程所擁有時,調用 lock
的線程將成功獲取該鎖并返回。如果當前線程已經擁有該鎖,此方法將立即返回。可以使用 isHeldByCurrentThread() 和
getHoldCount() 方法來檢查此情況是否發生。 ”
簡單來說,ReentrantLock有一個與鎖相關的獲取計
數器,如果擁有鎖的某個線程再次得到鎖,那么獲取計數器就加1,然后鎖需要被釋放兩次才能獲得真正釋放。這模仿了
synchronized 的語義;如果線程進入由線程已經擁有的監控器保護的 synchronized
塊,就允許線程繼續進行,當線程退出第二個(或者后續) synchronized 塊的時候,不釋放鎖,只有線程退出它進入的監控器保護的第一個
synchronized 塊時,才釋放鎖。
轉載注明出處:http://x- spirit.javaeye.com/、http: //www.aygfsteel.com/zhangwei217245/
ReentrantLock 類(重入鎖)實現了 Lock ,它擁有與 synchronized 相同的并發性和內存語義,但是添加了類似鎖投票、定時鎖等候和可中斷鎖等候的一些特性。此外,它還提供了在激烈爭用情況下更佳的性
能。(換句話說,當許多線程都想訪問共享資源時,JVM 可以花更少的時候來調度線程,把更多時間用在執行線程上。)
我們把 上面的例程改造一下:
從上面這個 程序我們看到:
對象鎖的獲得和釋放是由手工編碼完成的,所以獲得鎖和釋放鎖的時機比使用同步塊具有更好的可定制性。并
且通過程序的運行結果(運行結果忽略,請讀者根據例程自行觀察),我們可以發現,和使用同步塊的版本相比,結果是相同的。
轉載注明出處:http://x- spirit.javaeye.com/、http: //www.aygfsteel.com/zhangwei217245/
這說明兩點問題:
1. 新的ReentrantLock的確實現了和同步塊相同的語義功能。而對象鎖的獲得和釋放都可以由編碼
人員自行掌握。
2. 使用新的ReentrantLock,免去了為同步塊放置合適的對象鎖所要進行的考量。
3. 使用新的ReentrantLock,最佳的實踐就是結合try/finally塊來進行。在try塊之前使用lock方法,而
在finally中使用unlock方法。
轉載注明出處:http://x- spirit.javaeye.com/、http: //www.aygfsteel.com/zhangwei217245/
細心的讀者又發現了:
在我們的例程中,創建ReentrantLock實例的時候,我們的構造函數里面傳遞的參數是false。那么如果傳遞
true又回是什么結果呢?這里面又有什么奧秘呢?
請看本節的續 ———— Fair or Unfair? It is a question...
我想很多購買了《Java程序員面試寶典》
問:請對比synchronized與java.util.
答案:主要相同點:
主要不同點:
恩,讓我們先鄙視一下應試教育。
言歸正傳,我們先來看一個多線程程序。
public class ThreadDemo implements Runnable {
class Student {
private int age = 0;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Student student = new Student();
int count = 0;
public static void main(String[] args) {
ThreadDemo td = new ThreadDemo();
Thread t1 = new Thread(td, "a");
Thread t2 = new Thread(td, "b");
Thread t3 = new Thread(td, "c");
t1.start();
t2.start();
t3.start();
}
public void run() {
accessStudent();
}
public void accessStudent() {
String currentThreadName = Thread.currentThread().getName();
System.out.println(currentThreadName + " is running!");
synchronized (this) {//(1)使用同一個ThreadDemo對象作為同步鎖
System.out.println(currentThreadName + " got lock1@Step1!");
try {
count++;
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println(currentThreadName + " first Reading count:" + count);
}
}
System.out.println(currentThreadName + " release lock1@Step1!");
synchronized (this) {//(2)使用同一個ThreadDemo對象作為同步鎖
System.out.println(currentThreadName + " got lock2@Step2!");
try {
Random random = new Random();
int age = random.nextInt(100);
System.out.println("thread " + currentThreadName + " set age to:" + age);
this.student.setAge(age);
System.out.println("thread " + currentThreadName + " first read age is:" + this.student.getAge());
Thread.sleep(5000);
} catch (Exception ex) {
ex.printStackTrace();
} finally{
System.out.println("thread " + currentThreadName + " second read age is:" + this.student.getAge());
}
}
System.out.println(currentThreadName + " release lock2@Step2!");
}
}
轉載注明出處:http://x-
spirit.javaeye.com/、http:
//www.aygfsteel.com/zhangwei217245/
class Student {
private int age = 0;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Student student = new Student();
int count = 0;
public static void main(String[] args) {
ThreadDemo td = new ThreadDemo();
Thread t1 = new Thread(td, "a");
Thread t2 = new Thread(td, "b");
Thread t3 = new Thread(td, "c");
t1.start();
t2.start();
t3.start();
}
public void run() {
accessStudent();
}
public void accessStudent() {
String currentThreadName = Thread.currentThread().getName();
System.out.println(currentThreadName + " is running!");
synchronized (this) {//(1)使用同一個ThreadDemo對象作為同步鎖
System.out.println(currentThreadName + " got lock1@Step1!");
try {
count++;
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println(currentThreadName + " first Reading count:" + count);
}
}
System.out.println(currentThreadName + " release lock1@Step1!");
synchronized (this) {//(2)使用同一個ThreadDemo對象作為同步鎖
System.out.println(currentThreadName + " got lock2@Step2!");
try {
Random random = new Random();
int age = random.nextInt(100);
System.out.println("thread " + currentThreadName + " set age to:" + age);
this.student.setAge(age);
System.out.println("thread " + currentThreadName + " first read age is:" + this.student.getAge());
Thread.sleep(5000);
} catch (Exception ex) {
ex.printStackTrace();
} finally{
System.out.println("thread " + currentThreadName + " second read age is:" + this.student.getAge());
}
}
System.out.println(currentThreadName + " release lock2@Step2!");
}
}
運行結果:
a is running!
a got lock1@Step1!
b is running!
c is running!
a first Reading count:1
a release lock1@Step1!
a got lock2@Step2!
thread a set age to:76
thread a first read age is:76
thread a second read age is:76
a release lock2@Step2!
c got lock1@Step1!
c first Reading count:2
c release lock1@Step1!
c got lock2@Step2!
thread c set age to:35
thread c first read age is:35
thread c second read age is:35
c release lock2@Step2!
b got lock1@Step1!
b first Reading count:3
b release lock1@Step1!
b got lock2@Step2!
thread b set age to:91
thread b first read age is:91
thread b second read age is:91
b release lock2@Step2!
成功生成(總時間:30 秒)
a got lock1@Step1!
b is running!
c is running!
a first Reading count:1
a release lock1@Step1!
a got lock2@Step2!
thread a set age to:76
thread a first read age is:76
thread a second read age is:76
a release lock2@Step2!
c got lock1@Step1!
c first Reading count:2
c release lock1@Step1!
c got lock2@Step2!
thread c set age to:35
thread c first read age is:35
thread c second read age is:35
c release lock2@Step2!
b got lock1@Step1!
b first Reading count:3
b release lock1@Step1!
b got lock2@Step2!
thread b set age to:91
thread b first read age is:91
thread b second read age is:91
b release lock2@Step2!
成功生成(總時間:30 秒)
顯然,在這個程序中,
轉載注明出處:http://x- spirit.javaeye.com/、http: //www.aygfsteel.com/zhangwei217245/
我想一定有人會說:
那么好。我們把第二個同步塊中的對象鎖改為student(
a is running!
a got lock1@Step1!
b is running!
c is running!
a first Reading count:1
a release lock1@Step1!
a got lock2@Step2!
thread a set age to:73
thread a first read age is:73
c got lock1@Step1!
thread a second read age is:73
a release lock2@Step2!
c first Reading count:2
c release lock1@Step1!
c got lock2@Step2!
thread c set age to:15
thread c first read age is:15
b got lock1@Step1!
thread c second read age is:15
c release lock2@Step2!
b first Reading count:3
b release lock1@Step1!
b got lock2@Step2!
thread b set age to:19
thread b first read age is:19
thread b second read age is:19
b release lock2@Step2!
成功生成(總時間:21 秒)
a got lock1@Step1!
b is running!
c is running!
a first Reading count:1
a release lock1@Step1!
a got lock2@Step2!
thread a set age to:73
thread a first read age is:73
c got lock1@Step1!
thread a second read age is:73
a release lock2@Step2!
c first Reading count:2
c release lock1@Step1!
c got lock2@Step2!
thread c set age to:15
thread c first read age is:15
b got lock1@Step1!
thread c second read age is:15
c release lock2@Step2!
b first Reading count:3
b release lock1@Step1!
b got lock2@Step2!
thread b set age to:19
thread b first read age is:19
thread b second read age is:19
b release lock2@Step2!
成功生成(總時間:21 秒)
從 修改后的運行結果來看,顯然,由于同步塊的對象鎖不同了,
轉載注明出處:http://x- spirit.javaeye.com/、http: //www.aygfsteel.com/zhangwei217245/
可見,使用不同的對象鎖,在不同的同步塊中完成任務,
很多人看到這不禁要問:這和新的Lock框 架有什么關系?
別著急。我們這就來看一看。
synchronized塊的確不錯,
1. 它無法中斷一個正在等候獲得鎖的線程,
2.
轉載注明出處:http://x- spirit.javaeye.com/、http: //www.aygfsteel.com/zhangwei217245/
java.util.concurrent.lock 中的 Lock 框架是鎖定的一個抽象,它允許把鎖定的實現作為 Java 類,而不是作為語言的特性來實現。這就為 Lock 的多種實現留下了空間,各種實現可能有不同的調度算法、
JDK 官方文檔中提到:
ReentrantLock是“一個可重入的互斥鎖 Lock,它具有與使用 synchronized 方法和語句所訪問的隱式監視器鎖相同的一些基本行為和語義,
ReentrantLock 將由最近成功獲得鎖,并且還沒有釋放該鎖的線程所擁有。
簡單來說,
轉載注明出處:http://x- spirit.javaeye.com/、http: //www.aygfsteel.com/zhangwei217245/
ReentrantLock 類(重入鎖)實現了 Lock ,它擁有與 synchronized 相同的并發性和內存語義,但是添加了類似鎖投票、
我們把 上面的例程改造一下:
public class ThreadDemo implements Runnable {
class Student {
private int age = 0;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Student student = new Student();
int count = 0;
ReentrantLock lock1 = new ReentrantLock(false);
ReentrantLock lock2 = new ReentrantLock(false);
public static void main(String[] args) {
ThreadDemo td = new ThreadDemo();
for (int i = 1; i <= 3; i++) {
Thread t = new Thread(td, i + "");
t.start();
}
}
public void run() {
accessStudent();
}
public void accessStudent() {
String currentThreadName = Thread.currentThread().getName();
System.out.println(currentThreadName + " is running!");
lock1.lock();//使用重入鎖
System.out.println(currentThreadName + " got lock1@Step1!");
try {
count++;
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println(currentThreadName + " first Reading count:" + count);
lock1.unlock();
System.out.println(currentThreadName + " release lock1@Step1!");
}
lock2.lock();//使用另外一個不同的重入鎖
System.out.println(currentThreadName + " got lock2@Step2!");
try {
Random random = new Random();
int age = random.nextInt(100);
System.out.println("thread " + currentThreadName + " set age to:" + age);
this.student.setAge(age);
System.out.println("thread " + currentThreadName + " first read age is:" + this.student.getAge());
Thread.sleep(5000);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
System.out.println("thread " + currentThreadName + " second read age is:" + this.student.getAge());
lock2.unlock();
System.out.println(currentThreadName + " release lock2@Step2!");
}
}
}
class Student {
private int age = 0;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
Student student = new Student();
int count = 0;
ReentrantLock lock1 = new ReentrantLock(false);
ReentrantLock lock2 = new ReentrantLock(false);
public static void main(String[] args) {
ThreadDemo td = new ThreadDemo();
for (int i = 1; i <= 3; i++) {
Thread t = new Thread(td, i + "");
t.start();
}
}
public void run() {
accessStudent();
}
public void accessStudent() {
String currentThreadName = Thread.currentThread().getName();
System.out.println(currentThreadName + " is running!");
lock1.lock();//使用重入鎖
System.out.println(currentThreadName + " got lock1@Step1!");
try {
count++;
Thread.sleep(5000);
} catch (Exception e) {
e.printStackTrace();
} finally {
System.out.println(currentThreadName + " first Reading count:" + count);
lock1.unlock();
System.out.println(currentThreadName + " release lock1@Step1!");
}
lock2.lock();//使用另外一個不同的重入鎖
System.out.println(currentThreadName + " got lock2@Step2!");
try {
Random random = new Random();
int age = random.nextInt(100);
System.out.println("thread " + currentThreadName + " set age to:" + age);
this.student.setAge(age);
System.out.println("thread " + currentThreadName + " first read age is:" + this.student.getAge());
Thread.sleep(5000);
} catch (Exception ex) {
ex.printStackTrace();
} finally {
System.out.println("thread " + currentThreadName + " second read age is:" + this.student.getAge());
lock2.unlock();
System.out.println(currentThreadName + " release lock2@Step2!");
}
}
}
從上面這個 程序我們看到:
對象鎖的獲得和釋放是由手工編碼完成的,
轉載注明出處:http://x- spirit.javaeye.com/、http: //www.aygfsteel.com/zhangwei217245/
這說明兩點問題:
1. 新的ReentrantLock的確實現了和同步塊相同的語義功
2. 使用新的ReentrantLock,
3. 使用新的ReentrantLock,
轉載注明出處:http://x- spirit.javaeye.com/、http: //www.aygfsteel.com/zhangwei217245/
細心的讀者又發現了:
在我們的例程中,創建ReentrantLock實例的時候,
請看本節的續 ———— Fair or Unfair? It is a question...
使用 synchronized 其實仍由操作系統來調度線程的優先級,Lock 則是掌控在 java 程序中
Lock 用 lock() 和 unlock() 取代了 JVM 的 monitorenter 和 monitorexit 操作指令。
感謝黃鶯的深入分析~~~
不清楚你怎樣推出你的公式的。不過博客上談這個確實不方便。有時間可以面談。呵呵。。。
有一個問題,如果在lock方法之后在try之前出現一些異常,會不會導致lock沒有unlock而出現問題。
為什么一定要在lock方法之后和try塊之前加入一些有可能拋出異常的語句,而不對這些語句進行異常捕獲呢?這樣的編碼習慣難道可取嗎?
或者如果你覺得可以的話,也可以在try塊的第一行就寫lock方法。
這個與JVM實現有關吧?
樓主哪里得來的結論,請指教
請參閱:http://flierlu.spaces.live.com/Blog/cns!3B002B2C3F5C8E36!160.entry
文中介紹了一種基于預期的鎖定策略。
文中講到:
“事實上,JVM 可以根據運行時信息選擇性合并同類型鎖。隨著現在機器自動代碼生成的廣泛引用,可以預期這種基于行為對鎖進行合并的思路會非常有用。“、
“這些問題(同步很慢、把類和方法聲明為final可以提高性能、不可變對象是性能毒藥)的出現,往往是因為使用者對 JVM 的實現和優化思路不熟悉導致的。實際上 HotSpot 自從 JDK 1.3 版本以后,實際上有了非常大的進步,無論是從功能還是性能上,都已經遠遠超出了某些人的預期。而在可以預見的 Mustang 和 Dolphin 中,更高級和動態的優化還會不斷加入進來,并從 JVM 一級對應用產生透明的性能提升。”
實際上,看一下我在本篇文章中的第一個例程的運行結果,你就會發現,JVM的確是從語義上合并了兩段同步塊的鎖。而從常理上講,這樣的鎖分配策略在保證線程運行流暢性方面是相對比較優化的方案。