您好,登錄后才能下訂單哦!
Java中具有通過Synchronized實現的內置鎖,和ReentrantLock實現的顯示鎖,這兩種鎖各有各的好處,算是互有補充,今天就來做一個總結。
內置鎖獲得鎖和釋放鎖是隱式的,進入synchronized修飾的代碼就獲得鎖,走出相應的代碼就釋放鎖。
synchronized(list){ //獲得鎖
list.append();
list.count();
}//釋放鎖
通信
與Synchronized配套使用的通信方法通常有wait(),notify()。
wait()方法會立即釋放當前鎖,并進入等待狀態,等待到相應的notify并重新獲得鎖過后才能繼續執行;notify()不會立刻立刻釋放鎖,必須要等notify()所在線程執行完synchronized塊中的所有代碼才會釋放。用如下代碼來進行驗證:
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());
}
}
運行結果
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
可見讀線程開始運行,開始wait過后,寫線程才獲得鎖;寫線程走出同步塊而不是notify過后,讀線程才wait結束,亦即獲得鎖。所以notify不會釋放鎖,wait會釋放鎖。值得一提的是,notifyall()會通知等待隊列中的所有線程。
編碼
編碼模式比較簡單,單一,不必顯示的獲得鎖,釋放鎖,能降低因粗心忘記釋放鎖的錯誤。使用模式如下:
synchronized(object){
}
靈活性
性能
Synchronized在JDK1.5及之前性能(主要指吞吐率)比較差,擴展性也不如ReentrantLock。但是JDK1.6以后,修改了管理內置鎖的算法,使得Synchronized和標準的ReentrantLock性能差別不大。
ReentrantLock是顯示鎖,需要顯示進行 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()才能創建一個Condition。從上面的代碼可以看出,Synchronized能實現的通信方式,Condition都可以實現,功能類似的代碼寫在同一行中。而Condition的優秀之處在于它可以為多個線程間建立不同的Condition,比如對象的讀/寫Condition,隊列的空/滿Condition,在JDK源碼中的ArrayBlockingQueue中就使用了這個特性:
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要復雜一些,而且一定要記得在finally中釋放鎖而不是其他地方,這樣才能保證即使出了異常也能釋放鎖。
靈活性
性能
雖然Synchronized和標準的ReentrantLock性能差別不大,但是ReentrantLock還提供了一種非互斥的讀寫鎖,
也就是不強制每次最多只有一個線程能持有鎖,它會避免“讀/寫”沖突,“寫/寫”沖突,但是不會排除“讀/讀”沖突,
因為“讀/讀”并不影響數據的完整性,所以可以多個讀線程同時持有鎖,這樣在讀寫比較高的情況下,性能會有很大的提升。
下面用兩種鎖分別實現的線程安全的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);
}
}
讀寫鎖測試代碼:
List list = new LinkedList();
for (int i=0;i<10000;i++){
list.add(i);
}
RWLockList rwLockList = new RWLockList(list);//初始化數據
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");
同步鎖測試代碼:
List list = new LinkedList();
for (int i=0;i<10000;i++){
list.add(i);
}
SyncList syncList = new SyncList(list);//初始化數據
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");
結果:
RWLockList take 248ms
RWLockList take 255ms
RWLockList take 249ms
RWLockList take 224ms
SyncList take 351ms
SyncList take 367ms
SyncList take 315ms
SyncList take 323ms
可見讀寫鎖的確是優于純碎的互斥鎖
內置鎖最大優點是簡潔易用,顯示鎖最大優點是功能豐富,所以能用內置鎖就用內置鎖,在內置鎖功能不能滿足之時在考慮顯示鎖。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。