您好,登錄后才能下訂單哦!
Java中具有通過Synchronized實(shí)現(xiàn)的內(nèi)置鎖,和ReentrantLock實(shí)現(xiàn)的顯示鎖,這兩種鎖各有各的好處,算是互有補(bǔ)充,今天就來做一個(gè)總結(jié)。
內(nèi)置鎖獲得鎖和釋放鎖是隱式的,進(jìn)入synchronized修飾的代碼就獲得鎖,走出相應(yīng)的代碼就釋放鎖。
synchronized(list){ //獲得鎖
list.append();
list.count();
}//釋放鎖
通信
與Synchronized配套使用的通信方法通常有wait(),notify()。
wait()方法會(huì)立即釋放當(dāng)前鎖,并進(jìn)入等待狀態(tài),等待到相應(yīng)的notify并重新獲得鎖過后才能繼續(xù)執(zhí)行;notify()不會(huì)立刻立刻釋放鎖,必須要等notify()所在線程執(zhí)行完synchronized塊中的所有代碼才會(huì)釋放。用如下代碼來進(jìn)行驗(yàn)證:
public static void main(String[] args){
List list = new LinkedList();
Thread r = new Thread(new ReadList(list));
Thread w = new Thread(new WriteList(list));
r.start();
w.start();
}
class ReadList implements Runnable{
private List list;
public ReadList(List list){ this.list = list; }
@Override
public void run(){
System.out.println("ReadList begin at "+System.currentTimeMillis());
synchronized (list){
try {
Thread.sleep(1000);
System.out.println("list.wait() begin at "+System.currentTimeMillis());
list.wait();
System.out.println("list.wait() end at "+System.currentTimeMillis());
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println("ReadList end at "+System.currentTimeMillis());
}
}
class WriteList implements Runnable{
private List list;
public WriteList(List list){ this.list = list; }
@Override
public void run(){
System.out.println("WriteList begin at "+System.currentTimeMillis());
synchronized (list){
System.out.println("get lock at "+System.currentTimeMillis());
list.notify();
System.out.println("list.notify() at "+System.currentTimeMillis());
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("get out of block at "+System.currentTimeMillis());
}
System.out.println("WriteList end at "+System.currentTimeMillis());
}
}
運(yùn)行結(jié)果
ReadList begin at 1493650526582
WriteList begin at 1493650526582
list.wait() begin at 1493650527584
get lock at 1493650527584
list.notify() at 1493650527584
get out of block at 1493650529584
WriteList end at 1493650529584
list.wait() end at 1493650529584
ReadList end at 1493650529584
可見讀線程開始運(yùn)行,開始wait過后,寫線程才獲得鎖;寫線程走出同步塊而不是notify過后,讀線程才wait結(jié)束,亦即獲得鎖。所以notify不會(huì)釋放鎖,wait會(huì)釋放鎖。值得一提的是,notifyall()會(huì)通知等待隊(duì)列中的所有線程。
編碼
編碼模式比較簡(jiǎn)單,單一,不必顯示的獲得鎖,釋放鎖,能降低因粗心忘記釋放鎖的錯(cuò)誤。使用模式如下:
synchronized(object){
}
靈活性
性能
Synchronized在JDK1.5及之前性能(主要指吞吐率)比較差,擴(kuò)展性也不如ReentrantLock。但是JDK1.6以后,修改了管理內(nèi)置鎖的算法,使得Synchronized和標(biāo)準(zhǔn)的ReentrantLock性能差別不大。
ReentrantLock是顯示鎖,需要顯示進(jìn)行 lock 以及 unlock 操作。
通信
與ReentrantLock搭配的通行方式是Condition,如下:
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
condition.await();//this.wait();
condition.signal();//this.notify();
condition.signalAll();//this.notifyAll();
Condition是被綁定到Lock上的,必須使用lock.newCondition()才能創(chuàng)建一個(gè)Condition。從上面的代碼可以看出,Synchronized能實(shí)現(xiàn)的通信方式,Condition都可以實(shí)現(xiàn),功能類似的代碼寫在同一行中。而Condition的優(yōu)秀之處在于它可以為多個(gè)線程間建立不同的Condition,比如對(duì)象的讀/寫Condition,隊(duì)列的空/滿Condition,在JDK源碼中的ArrayBlockingQueue中就使用了這個(gè)特性:
public ArrayBlockingQueue(int capacity, boolean fair) {
if (capacity <= 0)
throw new IllegalArgumentException();
this.items = new Object[capacity];
lock = new ReentrantLock(fair);
notEmpty = lock.newCondition();
notFull = lock.newCondition();
}
public void put(E e) throws InterruptedException {
checkNotNull(e);
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
while (count == items.length)
notFull.await();
enqueue(e);
} finally {
lock.unlock();
}
}
public E take() throws InterruptedException {
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
while (count == 0)
notEmpty.await();
return dequeue();
} finally {
lock.unlock();
}
}
private void enqueue(E x) {
// assert lock.getHoldCount() == 1;
// assert items[putIndex] == null;
final Object[] items = this.items;
items[putIndex] = x;
if (++putIndex == items.length)
putIndex = 0;
count++;
notEmpty.signal();
}
private E dequeue() {
// assert lock.getHoldCount() == 1;
// assert items[takeIndex] != null;
final Object[] items = this.items;
@SuppressWarnings("unchecked")
E x = (E) items[takeIndex];
items[takeIndex] = null;
if (++takeIndex == items.length)
takeIndex = 0;
count--;
if (itrs != null)
itrs.elementDequeued();
notFull.signal();
return x;
}
編碼
Lock lock = new ReentrantLock();
lock.lock();
try{
}finally{
lock.unlock();
}
相比于Synchronized要復(fù)雜一些,而且一定要記得在finally中釋放鎖而不是其他地方,這樣才能保證即使出了異常也能釋放鎖。
靈活性
性能
雖然Synchronized和標(biāo)準(zhǔn)的ReentrantLock性能差別不大,但是ReentrantLock還提供了一種非互斥的讀寫鎖,
也就是不強(qiáng)制每次最多只有一個(gè)線程能持有鎖,它會(huì)避免“讀/寫”沖突,“寫/寫”沖突,但是不會(huì)排除“讀/讀”沖突,
因?yàn)椤白x/讀”并不影響數(shù)據(jù)的完整性,所以可以多個(gè)讀線程同時(shí)持有鎖,這樣在讀寫比較高的情況下,性能會(huì)有很大的提升。
下面用兩種鎖分別實(shí)現(xiàn)的線程安全的linkedlist:
class RWLockList {//讀寫鎖
private List list;
private final ReadWriteLock lock = new ReentrantReadWriteLock();
private final Lock readLock = lock.readLock();
private final Lock writeLock = lock.writeLock();
public RWLockList(List list){this.list = list;}
public int get(int k) {
readLock.lock();
try {
return (int)list.get(k);
} finally {
readLock.unlock();
}
}
public void put(int value) {
writeLock.lock();
try {
list.add(value);
} finally {
writeLock.unlock();
}
}
}
class SyncList {
private List list;
public SyncList(List list){this.list = list;}
public synchronized int get(int k){
return (int)list.get(k);
}
public synchronized void put(int value){
list.add(value);
}
}
讀寫鎖測(cè)試代碼:
List list = new LinkedList();
for (int i=0;i<10000;i++){
list.add(i);
}
RWLockList rwLockList = new RWLockList(list);//初始化數(shù)據(jù)
Thread writer = new Thread(new Runnable() {
@Override
public void run() {
for (int i=0;i<10000;i++){
rwLockList.put(i);
}
}
});
Thread reader1 = new Thread(new Runnable() {
@Override
public void run() {
for (int i=0;i<10000;i++){
rwLockList.get(i);
}
}
});
Thread reader2 = new Thread(new Runnable() {
@Override
public void run() {
for (int i=0;i<10000;i++){
rwLockList.get(i);
}
}
});
long begin = System.currentTimeMillis();
writer.start();reader1.start();reader2.start();
try {
writer.join();
reader1.join();
reader2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("RWLockList take "+(System.currentTimeMillis()-begin) + "ms");
同步鎖測(cè)試代碼:
List list = new LinkedList();
for (int i=0;i<10000;i++){
list.add(i);
}
SyncList syncList = new SyncList(list);//初始化數(shù)據(jù)
Thread writerS = new Thread(new Runnable() {
@Override
public void run() {
for (int i=0;i<10000;i++){
syncList.put(i);
}
}
});
Thread reader1S = new Thread(new Runnable() {
@Override
public void run() {
for (int i=0;i<10000;i++){
syncList.get(i);
}
}
});
Thread reader2S = new Thread(new Runnable() {
@Override
public void run() {
for (int i=0;i<10000;i++){
syncList.get(i);
}
}
});
long begin1 = System.currentTimeMillis();
writerS.start();reader1S.start();reader2S.start();
try {
writerS.join();
reader1S.join();
reader2S.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("SyncList take "+(System.currentTimeMillis()-begin1) + "ms");
結(jié)果:
RWLockList take 248ms
RWLockList take 255ms
RWLockList take 249ms
RWLockList take 224ms
SyncList take 351ms
SyncList take 367ms
SyncList take 315ms
SyncList take 323ms
可見讀寫鎖的確是優(yōu)于純碎的互斥鎖
內(nèi)置鎖最大優(yōu)點(diǎn)是簡(jiǎn)潔易用,顯示鎖最大優(yōu)點(diǎn)是功能豐富,所以能用內(nèi)置鎖就用內(nèi)置鎖,在內(nèi)置鎖功能不能滿足之時(shí)在考慮顯示鎖。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。