synchronized與Lock的區別
阿新 • • 發佈:2019-05-14
util 可重入 方法 ted ace ber get ner ack
兩者區別:
1.首先synchronized是java內置關鍵字,在jvm層面,Lock是個java類;
2.synchronized無法判斷是否獲取鎖的狀態,Lock可以判斷是否獲取到鎖;
3.synchronized會自動釋放鎖(a 線程執行完同步代碼會釋放鎖 ;b 線程執行過程中發生異常會釋放鎖),Lock需在finally中手工釋放鎖(unlock()方法釋放鎖),否則容易造成線程死鎖;
4.用synchronized關鍵字的兩個線程1和線程2,如果當前線程1獲得鎖,線程2線程等待。如果線程1阻塞,線程2則會一直等待下去,而Lock鎖就不一定會等待下去,如果嘗試獲取不到鎖,線程可以不用一直等待就結束了;
5.synchronized的鎖可重入、不可中斷、非公平,而Lock鎖可重入、可判斷、可公平(兩者皆可)
6.Lock鎖適合大量同步的代碼的同步問題,synchronized鎖適合代碼少量的同步問題。
小例子:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
package com.cn.test.thread.lock;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class LockTest {
private Lock lock = new ReentrantLock();
/*
* 使用完畢釋放後其他線程才能獲取鎖
*/
public void lockTest(Thread thread) {
lock.lock(); //獲取鎖
try {
System.out.println( "線程" +thread.getName() + "獲取當前鎖" ); //打印當前鎖的名稱
Thread.sleep( 2000 ); //為看出執行效果,是線程此處休眠2秒
} catch (Exception e) {
System.out.println( "線程" +thread.getName() + "發生了異常釋放鎖" );
} finally {
System.out.println( "線程" +thread.getName() + "執行完畢釋放鎖" );
lock.unlock(); //釋放鎖
}
}
public static void main(String[] args) {
LockTest lockTest = new LockTest();
//聲明一個線程 “線程一”
Thread thread1 = new Thread( new Runnable() {
@Override
public void run() {
lockTest.lockTest(Thread.currentThread());
}
}, "thread1" );
//聲明一個線程 “線程二”
Thread thread2 = new Thread( new Runnable() {
@Override
public void run() {
lockTest.lockTest(Thread.currentThread());
}
}, "thread2" );
// 啟動2個線程
thread2.start();
thread1.start();
}
}
|
執行結果:
package com.cn.test.thread.lock; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class LockTest { private Lock lock = new ReentrantLock(); /* * 嘗試獲取鎖 tryLock() 它表示用來嘗試獲取鎖,如果獲取成功,則返回true,如果獲取失敗(即鎖已被其他線程獲取),則返回false */ public void tryLockTest(Thread thread) { if(lock.tryLock()) { //嘗試獲取鎖 try { System.out.println("線程"+thread.getName() + "獲取當前鎖"); //打印當前鎖的名稱 Thread.sleep(2000);//為看出執行效果,是線程此處休眠2秒 } catch (Exception e) { System.out.println("線程"+thread.getName() + "發生了異常釋放鎖"); }finally { System.out.println("線程"+thread.getName() + "執行完畢釋放鎖"); lock.unlock(); //釋放鎖 } }else{ System.out.println("我是線程"+Thread.currentThread().getName()+"當前鎖被別人占用,我無法獲取"); } } public static void main(String[] args) { LockTest lockTest = new LockTest(); Thread thread1 = new Thread(new Runnable() { @Override public void run() { lockTest.tryLockTest(Thread.currentThread()); } }, "thread1"); //聲明一個線程 “線程二” Thread thread2 = new Thread(new Runnable() { @Override public void run() { lockTest.tryLockTest(Thread.currentThread()); } }, "thread2"); // 啟動2個線程 thread2.start(); thread1.start(); } }
執行結果:
package com.cn.test.thread.lock; import java.util.concurrent.TimeUnit; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class LockTest { private Lock lock = new ReentrantLock(); public void tryLockParamTest(Thread thread) throws InterruptedException { if(lock.tryLock(3000, TimeUnit.MILLISECONDS)) { //嘗試獲取鎖 獲取不到鎖,就等3秒,如果3秒後還是獲取不到就返回false try { System.out.println("線程"+thread.getName() + "獲取當前鎖"); //打印當前鎖的名稱 Thread.sleep(4000);//為看出執行效果,是線程此處休眠2秒 } catch (Exception e) { System.out.println("線程"+thread.getName() + "發生了異常釋放鎖"); }finally { System.out.println("線程"+thread.getName() + "執行完畢釋放鎖"); lock.unlock(); //釋放鎖 } }else{ System.out.println("我是線程"+Thread.currentThread().getName()+"當前鎖被別人占用,等待3s後仍無法獲取,放棄"); } } public static void main(String[] args) { LockTest lockTest = new LockTest(); Thread thread1 = new Thread(new Runnable() { @Override public void run() { try { lockTest.tryLockParamTest(Thread.currentThread()); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }, "thread1"); //聲明一個線程 “線程二” Thread thread2 = new Thread(new Runnable() { @Override public void run() { try { lockTest.tryLockParamTest(Thread.currentThread()); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } }, "thread2"); // 啟動2個線程 thread2.start(); thread1.start(); } }
執行結果:
因為此時線程1休眠了4秒,線程2等待了3秒還沒有獲取到就放棄獲取鎖了,執行結束
將方法中的 Thread.sleep(4000)改為Thread.sleep(2000)執行結果如下:
因為此時線程1休眠了2秒,線程2等待了3秒的期間線程1釋放了鎖,此時線程2獲取到鎖,線程2就可以執行了
synchronized與Lock的區別