上一篇:Java線程(六)
Lock是java.util.concurrent.locks包下的介面,Lock 實現提供了比使用synchronized 方法和語句可獲得的更廣泛的鎖定操作,它能以更優雅的方式處理線程同步問題,我們拿Java線程(二)中的一個例子簡單的實現一下和sychronized一樣的效果,代碼如下:
public class LockTest {public static void main(String[] args) {final Outputter1 output = new Outputter1();new Thread() {public void run() {output.output("zhangsan");};}.start();new Thread() {public void run() {output.output("lisi");};}.start();}}class Outputter1 {private Lock lock = new ReentrantLock();// 鎖對象public void output(String name) {// TODO 線程輸出方法lock.lock();// 得到鎖try {for(int i = 0; i < name.length(); i++) {System.out.print(name.charAt(i));}} finally {lock.unlock();// 釋放鎖}}}
這樣就實現了和sychronized一樣的同步效果,需要注意的是,用sychronized修飾的方法或者語句塊在代碼執行完之後鎖自動釋放,而是用Lock需要我們手動釋放鎖,所以為了保證鎖最終被釋放(發生異常情況),要把互斥區放在try內,釋放鎖放在finally內。
如果說這就是Lock,那麼它不能成為同步問題更完美的處理方式,下面要介紹的是讀寫鎖(ReadWriteLock),我們會有一種需求,在對資料進行讀寫的時候,為了保證資料的一致性和完整性,需要讀和寫是互斥的,寫和寫是互斥的,但是讀和讀是不需要互斥的,這樣讀和讀不互斥效能更高些,來看一下不考慮互斥情況的代碼原型:
public class ReadWriteLockTest {public static void main(String[] args) {final Data data = new Data();for (int i = 0; i < 3; i++) {new Thread(new Runnable() {public void run() {for (int j = 0; j < 5; j++) {data.set(new Random().nextInt(30));}}}).start();}for (int i = 0; i < 3; i++) {new Thread(new Runnable() {public void run() {for (int j = 0; j < 5; j++) {data.get();}}}).start();}}}class Data {private int data;// 共用資料public void set(int data) {System.out.println(Thread.currentThread().getName() + "準備寫入資料");try {Thread.sleep(20);} catch (InterruptedException e) {e.printStackTrace();}this.data = data;System.out.println(Thread.currentThread().getName() + "寫入" + this.data);}public void get() {System.out.println(Thread.currentThread().getName() + "準備讀取資料");try {Thread.sleep(20);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName() + "讀取" + this.data);}}
部分輸出結果:
Thread-1準備寫入資料Thread-3準備讀取資料Thread-2準備寫入資料Thread-0準備寫入資料Thread-4準備讀取資料Thread-5準備讀取資料Thread-2寫入12Thread-4讀取12Thread-5讀取5Thread-1寫入12
我們要實現寫入和寫入互斥,讀取和寫入互斥,讀取和讀取互斥,在set和get方法加入sychronized修飾符:
public synchronized void set(int data) {...}public synchronized void get() {...}
部分輸出結果:
Thread-0準備寫入資料Thread-0寫入9Thread-5準備讀取資料Thread-5讀取9Thread-5準備讀取資料Thread-5讀取9Thread-5準備讀取資料Thread-5讀取9Thread-5準備讀取資料Thread-5讀取9
我們發現,雖然寫入和寫入互斥了,讀取和寫入也互斥了,但是讀取和讀取之間也互斥了,不能並發執行,效率較低,用讀寫鎖實現代碼如下:
class Data {private int data;// 共用資料private ReadWriteLock rwl = new ReentrantReadWriteLock();public void set(int data) {rwl.writeLock().lock();// 取到寫鎖try {System.out.println(Thread.currentThread().getName() + "準備寫入資料");try {Thread.sleep(20);} catch (InterruptedException e) {e.printStackTrace();}this.data = data;System.out.println(Thread.currentThread().getName() + "寫入" + this.data);} finally {rwl.writeLock().unlock();// 釋放寫鎖}}public void get() {rwl.readLock().lock();// 取到讀鎖try {System.out.println(Thread.currentThread().getName() + "準備讀取資料");try {Thread.sleep(20);} catch (InterruptedException e) {e.printStackTrace();}System.out.println(Thread.currentThread().getName() + "讀取" + this.data);} finally {rwl.readLock().unlock();// 釋放讀鎖}}}
部分輸出結果:
Thread-4準備讀取資料Thread-3準備讀取資料Thread-5準備讀取資料Thread-5讀取18Thread-4讀取18Thread-3讀取18Thread-2準備寫入資料Thread-2寫入6Thread-2準備寫入資料Thread-2寫入10Thread-1準備寫入資料Thread-1寫入22Thread-5準備讀取資料
從結果可以看出實現了我們的需求,這隻是鎖的基本用法,鎖的機制還需要繼續深入學習。
下一篇:Java線程(八)
本文來自:高爽|Coder,原文地址:http://blog.csdn.net/ghsau/article/details/7461369。