av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

Java 多線程Synchronized和Lock的區別

瀏覽:31日期:2022-08-18 18:10:52
引言

在多線程中,為了使線程安全,我們經常會使用synchronized和Lock進行代碼同步和加鎖,但是具體兩者有什么區別,什么場景下適合用什么可能還不大清楚,主要的區別大致如下:

區別

1、synchronized是java關鍵字,而Lock是java中的一個接口

2、synchronized會自動釋放鎖,而Lock必須手動釋放鎖

3、synchronized是不可中斷的,Lock可以中斷也可以不中斷

4、通過Lock可以知道線程有沒有拿到鎖,而synchronized不能

5、synchronized能鎖住方法和代碼塊,而Lock只能鎖住代碼塊

6、Lock可以使用讀鎖提高多線程讀效率

7、synchronized是非公平鎖,ReentranLock可以控制是否公平鎖

從Lock接口中我們可以看到主要有5個方法,這些方法的功能從注釋中可以看出:

lock():獲取鎖,如果鎖被暫用則一直等待unlock():釋放鎖tryLock(): 注意返回類型是boolean,如果獲取鎖的時候鎖被占用就返回false,否則返回truetryLock(long time, TimeUnit unit):比起tryLock()就是給了一個時間期限,保證等待參數時間lockInterruptibly():用該鎖的獲得方式,如果線程在獲取鎖的階段進入了等待,那么可以中斷此線程,先去做別的事 通過 以上的解釋,大致可以解釋在上個部分中“鎖類型(lockInterruptibly())”,“鎖狀態(tryLock())”等問題,還有就是前面子所獲取的過程我所寫的“大致就是可以嘗試獲得鎖,線程可以不會一直等待”用了“可以”的原因。lock():

public class LockTest { private Lock lock = new ReentrantLock(); private void method(Thread thread) { lock.lock(); try { System.out.println(thread.getName() + ' has gotten the lock!'); } catch (Exception e) { e.printStackTrace(); } finally { System.out.println(thread.getName() + ' has unlocked the lock!'); lock.unlock(); } } public static void main(String[] args) { final LockTest test = new LockTest(); Thread t1 = new Thread(new Runnable() { @Override public void run() {test.method(Thread.currentThread()); } }, 't1'); Thread t2 = new Thread(new Runnable() { @Override public void run() {test.method(Thread.currentThread()); } }, 't2'); t1.start(); t2.start(); }}

運行結果:

t1 has gotten the lock!t1 has unlocked the lock!t2 has gotten the lock!t2 has unlocked the lock!tryLock():

public class LockTest { private Lock lock = new ReentrantLock(); private void method(Thread thread) { if (lock.tryLock()) { lock.lock(); try {System.out.println(thread.getName() + ' has gotten the lock!'); } catch (Exception e) {e.printStackTrace(); } finally {System.out.println(thread.getName() + ' has unlocked the lock!');lock.unlock(); } } else { System.out.println('I’m '+thread.getName()+'. Someone has gotten the lock!'); } } public static void main(String[] args) { LockTest test = new LockTest(); Thread t1 = new Thread(() -> test.method(Thread.currentThread()), 't1'); Thread t2 = new Thread(new Runnable() { @Override public void run() {test.method(Thread.currentThread()); } }, 't2'); t1.start(); t2.start(); }}

運行結果:

t1 has gotten the lock!t1 has unlocked the lock!I’m t2. Someone has gotten the lock!

看到這里相信大家也都會使用如何使用Lock了吧,關于tryLock(long time, TimeUnit unit)和lockInterruptibly()不再贅述。前者主要存在一個等待時間,在測試代碼中寫入一個等待時間,后者主要是等待中斷,會拋出一個中斷異常,常用度不高,喜歡探究可以自己深入研究。

以上就是Java 多線程Synchronized和Lock的區別的詳細內容,更多關于Java 多線程Synchronized和Lock的資料請關注好吧啦網其它相關文章!

標簽: Java
相關文章:
主站蜘蛛池模板: 精品日韩在线观看 | 国产伦精品一区二区三区视频网站 | 在线观看免费毛片 | 亚洲精品字幕 | 黄色三级在线 | 四虎地址 | 美利坚合众国av | 欧美1区2区 | 亚洲日本国产 | 艳妇乳肉豪妇荡乳 | 国产精品日韩在线 | 天天躁日日躁狠狠躁av麻豆男男 | 97视频免费在线观看 | 日本中文字幕在线播放 | 久久久久久久网站 | 深夜福利影院 | 一级免费视频 | 在线观看免费毛片 | 欧美日韩在线免费观看 | 亚洲av毛片成人精品 | 伊人网在线视频 | 欧美 日韩 国产 成人 在线 | 天天干天天色 | 黄av在线| 欧美视频在线观看免费 | 久久久久久久国产精品 | 日韩专区中文字幕 | 一级片免费播放 | 国产一区二区影院 | 日本一级淫片色费放 | 黑人操亚洲人 | 一级黄色片免费观看 | 密乳av| 日本黄a三级三级三级 | 国产日韩精品一区二区 | 中文字幕欧美在线 | 日韩一二三 | 欧美日本在线 | 黄色片久久 | 亚洲欧美成人 | 黄色网址在线免费观看 |