国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術(shù)文章
文章詳情頁

Java 多線程Synchronized和Lock的區(qū)別

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

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

區(qū)別

1、synchronized是java關(guān)鍵字,而Lock是java中的一個(gè)接口

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

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

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

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

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

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

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

lock():獲取鎖,如果鎖被暫用則一直等待unlock():釋放鎖tryLock(): 注意返回類型是boolean,如果獲取鎖的時(shí)候鎖被占用就返回false,否則返回truetryLock(long time, TimeUnit unit):比起tryLock()就是給了一個(gè)時(shí)間期限,保證等待參數(shù)時(shí)間lockInterruptibly():用該鎖的獲得方式,如果線程在獲取鎖的階段進(jìn)入了等待,那么可以中斷此線程,先去做別的事 通過 以上的解釋,大致可以解釋在上個(gè)部分中“鎖類型(lockInterruptibly())”,“鎖狀態(tài)(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(); }}

運(yùn)行結(jié)果:

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(); }}

運(yùn)行結(jié)果:

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

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

以上就是Java 多線程Synchronized和Lock的區(qū)別的詳細(xì)內(nèi)容,更多關(guān)于Java 多線程Synchronized和Lock的資料請關(guān)注好吧啦網(wǎng)其它相關(guān)文章!

標(biāo)簽: Java
相關(guān)文章:
主站蜘蛛池模板: 午夜a一级毛片一.成 | 91成年人免费视频 | 亚洲第一在线 | 亚洲国产美女视频 | 国内自拍在线观看 | 国产亚洲一区二区三区在线 | 狠狠色狠狠色狠狠五月ady | 亚洲精品资源在线 | 国产偷怕自拍 | 久久成人动漫 | 7777在线| 日本一区二区三区在线 视频 | 一区二区三区精品国产 | 久久久精品2018免费观看 | 日本高清在线精品一区二区三区 | 亚洲精品色 | 欧美三级观看 | 久久精品国产99国产精品亚洲 | 日本免费一区二区三区看片 | 国产视频日韩 | 国产大乳孕妇喷奶水在线观看 | 精品无码三级在线观看视频 | 亚洲mm8成为人影院 亚洲m男在线中文字幕 | 日韩一区二区三区在线播放 | 亚洲天码中文字幕第一页 | 久久99热成人精品国产 | 国产v片在线播放免费观 | 三级黄色在线播放 | 国产三级在线播放线 | 97免费视频免费视频 | 亚洲国产高清人在线 | 日p免费视频 | 国产精品爽爽va在线观看无码 | 国产精品99久久免费观看 | 中文字幕在线不卡 | 欧美精品v日韩精品v国产精品 | 国产综合在线观看视频 | 国产乱码精品一区二区三区四川人 | 国产99网站 | 日本三级香港三级网站 | 国产精品三级a三级三级午夜 |