Java中wait與sleep的區(qū)別講解(wait有參及無(wú)參區(qū)別)
1. wait() 與wait( long timeout ) 區(qū)別
public class WaitDemo4 { public static void main(String[] args) { Object lock = new Object(); Object lock2 = new Object(); new Thread(() -> { System.out.println("線程1: 開(kāi)始執(zhí)行" + LocalDateTime.now()); synchronized (lock) { try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("線程1: 執(zhí)行完成" + LocalDateTime.now()); } },"無(wú)參wait線程").start(); new Thread(() -> { System.out.println("線程2: 開(kāi)始執(zhí)行" + LocalDateTime.now()); synchronized (lock2) { try { lock2.wait(1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("線程2: 執(zhí)行完成" + LocalDateTime.now()); } },"有參wait線程").start(); } } 輸出: 線程2: 開(kāi)始執(zhí)行2022-04-12T12:13:57.130 線程1: 開(kāi)始執(zhí)行2022-04-12T12:13:57.130 線程2: 執(zhí)行完成2022-04-12T12:13:58.130
不同點(diǎn):
1.wait( long timeout) :當(dāng)線程超過(guò)了設(shè)置時(shí)間之后,自動(dòng)恢復(fù)執(zhí)行;而wait() 無(wú)線等待狀態(tài)。
2. 使用無(wú)參的wait方法,線程會(huì)進(jìn)入WAITING; 使用有參的wait方法,線程會(huì)進(jìn)入TIMED_WAITING。
public class WaitDemo5 { public static void main(String[] args) { Object lock = new Object(); Object lock2 = new Object(); new Thread(() -> { synchronized (lock2) { System.out.println("線程2: 開(kāi)始執(zhí)行" + LocalDateTime.now()); try { lock2.wait(60 * 60 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("線程2: 執(zhí)行完成" + LocalDateTime.now()); } },"有參wait線程").start(); new Thread(() -> { try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (lock2) { System.out.println("喚醒線程2"); lock2.notify(); } }).start(); } } 輸出: 線程2: 開(kāi)始執(zhí)行2022-04-12T12:28:23.200 喚醒線程2 線程2: 執(zhí)行完成2022-04-12T12:28:24.169
public class WaitDemo6 { public static void main(String[] args) { Object lock = new Object(); new Thread(() -> { System.out.println("線程1: 開(kāi)始執(zhí)行" + LocalDateTime.now()); synchronized (lock) { try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("線程1: 執(zhí)行完成" + LocalDateTime.now()); } },"無(wú)參wait線程").start(); new Thread(() -> { System.out.println("線程2: 開(kāi)始執(zhí)行" + LocalDateTime.now()); synchronized (lock) { try { lock.wait(60 * 60 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("線程2: 執(zhí)行完成" + LocalDateTime.now()); } },"有參wait線程").start(); new Thread(() -> { try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (lock) { System.out.println("喚醒所有線程"); lock.notifyAll(); } }).start(); } } 輸出: 線程1: 開(kāi)始執(zhí)行2022-04-12T12:34:34.317 線程2: 開(kāi)始執(zhí)行2022-04-12T12:34:34.317 喚醒所有線程 線程2: 執(zhí)行完成2022-04-12T12:34:35.295 線程1: 執(zhí)行完成2022-04-12T12:34:35.295
共同點(diǎn):
1. 無(wú)論是有參的wait方法還是無(wú)參的wait方法,它都可以使用當(dāng)前線程進(jìn)入休眠狀態(tài)。
2.無(wú)論是有參的wait方法還是無(wú)參的wait方法,它都可以使用notify / ontifyAll進(jìn)行喚醒。
2. wait(0) 與 sleep(0)區(qū)別
public class WaitSleepDemo7 { public static void main(String[] args) { Object lock = new Object(); Thread t1 = new Thread(() -> { synchronized (lock) { System.out.println("線程1:開(kāi)始執(zhí)行"); try { lock.wait(0); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("線程1:執(zhí)行結(jié)束"); } },"wait(0)"); t1.start(); Thread t2 = new Thread(() -> { System.out.println("線程2:開(kāi)始執(zhí)行"); try { Thread.sleep(0); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("線程2:執(zhí)行結(jié)束"); }, "sleep(0)"); t2.start(); } } 輸出: 線程1:開(kāi)始執(zhí)行 線程2:開(kāi)始執(zhí)行 線程2:執(zhí)行結(jié)束
wait (0) : 無(wú)限期等待下去,相當(dāng)于wait();
sleep(0) :相當(dāng)于Thread.yeild() , 讓出CPU執(zhí)行權(quán),重新調(diào)度,但是sleep(0) 會(huì)繼續(xù)執(zhí)行。
3. wait 和sleep 釋放代碼
wait 和 sleep 在有所的情況下的鎖處理行為是完全不同的:
public class WaitSleepDemo8 { public static void main(String[] args) throws InterruptedException { Object lock = new Object(); Object lock2 = new Object(); Thread t1 = new Thread(() -> { synchronized (lock) { System.out.println("線程1:開(kāi)始執(zhí)行"); try { lock.wait(3 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("線程1:結(jié)束執(zhí)行"); } }, "wait"); t1.start(); Thread t2 = new Thread(() -> { synchronized (lock2) { System.out.println("線程2:開(kāi)始執(zhí)行"); try { Thread.sleep(3 * 1000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("線程2:結(jié)束執(zhí)行"); } }, "sleep"); t2.start(); // 創(chuàng)建 2 個(gè)線程,先讓線程休眠 1s 之后,嘗試獲取,看能不能獲取到鎖 // 如果可以獲取到鎖,說(shuō)明休眠時(shí)線程是釋放鎖的,而如果獲取不到鎖,說(shuō)明是不釋放鎖 Thread t3 = new Thread(() -> { try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("嘗試獲取 wait 方法的鎖"); synchronized (lock) { System.out.println("成功獲取 wait 的鎖"); } }, "wait2"); t3.start(); Thread t4 = new Thread(() -> { try { TimeUnit.SECONDS.sleep(0); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("嘗試獲取 sleep 方法的鎖"); synchronized (lock2) { System.out.println("成功獲取 sleep 的鎖"); } }, "sleep2"); t4.start(); } } 輸出: 線程1:開(kāi)始執(zhí)行 線程2:開(kāi)始執(zhí)行 嘗試獲取 sleep 方法的鎖 嘗試獲取 wait 方法的鎖 成功獲取 wait 的鎖 線程1:結(jié)束執(zhí)行 線程2:結(jié)束執(zhí)行 成功獲取 sleep 的鎖
wait方法(不管是有參還是無(wú)參)在執(zhí)行的時(shí)候都會(huì)釋放鎖;而sleep 方法不會(huì)釋放鎖。
4. wait 與 sleep 區(qū)別
相同點(diǎn):
1. 都是可以讓線程進(jìn)入休眠
2. 都可以響應(yīng)Interrupt(中斷)請(qǐng)求
不同點(diǎn):
1. wait必須配合synchronized一起使用;而sleep不需要。
2. wait 屬于Object(對(duì)象)的方法;而sleep屬于Thread(線程)的方法。
3. sleep 不釋放鎖;而wait釋放鎖。
4. sleep 必須要傳遞一個(gè)數(shù)值類型的參數(shù);而wait可以不傳參。
5. sleep 讓線程進(jìn)入到TIMED_WAITING狀態(tài);而無(wú)參的wait方法讓線程進(jìn)入了WAITING狀態(tài)。
6. 一般情況下,sleep只能等待超時(shí)時(shí)間之后再回復(fù)執(zhí)行;而wait可以接受notify / notifiAll之后就緒執(zhí)行。
(MS):
1.為什么 wait 釋放鎖? sleep 不釋放鎖?
【JVM 強(qiáng)制語(yǔ)法檢查,wait ?法默認(rèn)等待?期限】
2.為什么 wait 要放在 Object 中?
【wait 使?要加鎖,也就是要操作鎖,鎖是針對(duì)對(duì)象級(jí)別的??線程級(jí)別的,線程和對(duì)象是?對(duì)多,所以 wait 最便利的?式是放在 Object 中】
到此這篇關(guān)于Java中wait與sleep的區(qū)別講解(wait有參及無(wú)參區(qū)別)的文章就介紹到這了,更多相關(guān)java wait與sleep內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- java中sleep方法和wait方法的五個(gè)區(qū)別
- Java線程中sleep和wait的區(qū)別詳細(xì)介紹
- Java中sleep()與wait()的區(qū)別總結(jié)
- Java面試題篇之Sleep()方法與Wait()方法的區(qū)別詳解
- 詳解Java中wait和sleep的區(qū)別
- 詳解Java中的sleep()和wait()的區(qū)別
- java sleep()和wait()的區(qū)別點(diǎn)總結(jié)
- Java詳細(xì)分析sleep和wait方法有哪些區(qū)別
- java面試突擊之sleep和wait有什么區(qū)別詳析
- Java中wait()與sleep()兩者的不同深入解析
相關(guān)文章
SpringBoot實(shí)現(xiàn)返回值數(shù)據(jù)脫敏的步驟詳解
這篇文章主要給大家介紹一下SpringBoot實(shí)現(xiàn)返回值數(shù)據(jù)脫敏的步驟,文章通過(guò)代碼示例介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的朋友可以參考下2023-07-07Java8優(yōu)雅的字符串拼接工具類StringJoiner實(shí)例代碼
這篇文章主要給大家介紹了關(guān)于Java8優(yōu)雅的字符串拼接工具類StringJoiner的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-02-02Spring 使用JavaConfig實(shí)現(xiàn)配置的方法步驟
這篇文章主要介紹了Spring 使用JavaConfig實(shí)現(xiàn)配置的方法步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01SSM框架整合之Spring+SpringMVC+MyBatis實(shí)踐步驟
大家都知道Spring是一個(gè)輕量級(jí)的控制反轉(zhuǎn)(IoC)和面向切面(AOP)的容器框架,本文主要介紹三大框架的整合包含spring和mybatis的配置文件,還有spring-mvc的配置文件的詳細(xì)介紹,通過(guò)項(xiàng)目實(shí)踐步驟給大家詳細(xì)介紹,感興趣的朋友一起看看吧2021-06-06SpringSecurity定義多個(gè)過(guò)濾器鏈的操作代碼
Spring?Security?是?Spring家族中的一個(gè)安全管理框架。相比與另外一個(gè)安全框架Shiro,它提供了更豐富的功能,社區(qū)資源也比Shiro豐富,今天通過(guò)本文給大家介紹SpringSecurity定義多個(gè)過(guò)濾器鏈的實(shí)例,感興趣的朋友跟隨小編一起看看吧2023-04-04Idea中如何查看SpringSecurity各Filter信息
這篇文章主要介紹了Idea中如何查看SpringSecurity各Filter信息,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01詳解SpringBoot?統(tǒng)一后端返回格式的方法
今天我們來(lái)聊一聊在基于SpringBoot前后端分離開(kāi)發(fā)模式下,如何友好的返回統(tǒng)一的標(biāo)準(zhǔn)格式以及如何優(yōu)雅的處理全局異常,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2022-05-05Spring七大事務(wù)傳遞機(jī)制深入分析實(shí)現(xiàn)原理
實(shí)際項(xiàng)目開(kāi)發(fā)中,如果涉及到多張表操作時(shí),為了保證業(yè)務(wù)數(shù)據(jù)的一致性,大家一般都會(huì)采用事務(wù)機(jī)制,好多小伙伴可能只是簡(jiǎn)單了解一下,遇到事務(wù)失效的情況,便會(huì)無(wú)從下手,下面這篇文章主要給大家介紹了關(guān)于Spring事務(wù)傳遞機(jī)制的相關(guān)資料,需要的朋友可以參考下2023-03-03SpringBoot日期格式轉(zhuǎn)換之配置全局日期格式轉(zhuǎn)換器的實(shí)例詳解
這篇文章主要介紹了SpringBoot日期格式轉(zhuǎn)換之配置全局日期格式轉(zhuǎn)換器的實(shí)例詳解,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12