Java中sleep()與wait()的區(qū)別總結(jié)
前言
對于sleep()
方法,我們首先要知道該方法是屬于Thread類中的。而wait()
方法,則是屬于Object類中的。
sleep()
方法導(dǎo)致了程序暫停執(zhí)行指定的時間,讓出cpu該其他線程,但是他的監(jiān)控狀態(tài)依然保持者,當(dāng)指定的時間到了又會自動恢復(fù)運(yùn)行狀態(tài)。
在調(diào)用sleep()
方法的過程中,線程不會釋放對象鎖。
而當(dāng)調(diào)用wait()
方法的時候,線程會放棄對象鎖,進(jìn)入等待此對象的等待鎖定池,只有針對此對象調(diào)用notify()
方法后本線程才進(jìn)入對象鎖定池準(zhǔn)備
獲取對象鎖進(jìn)入運(yùn)行狀態(tài)。
什么意思呢?
舉個列子說明:
/** * */ package com.b510.test; /** * java中的sleep()和wait()的區(qū)別 * @author Hongten * @date 2013-12-10 */ public class TestD { public static void main(String[] args) { new Thread(new Thread1()).start(); try { Thread.sleep(5000); } catch (Exception e) { e.printStackTrace(); } new Thread(new Thread2()).start(); } private static class Thread1 implements Runnable{ @Override public void run(){ synchronized (TestD.class) { System.out.println("enter thread1..."); System.out.println("thread1 is waiting..."); try { //調(diào)用wait()方法,線程會放棄對象鎖,進(jìn)入等待此對象的等待鎖定池 TestD.class.wait(); } catch (Exception e) { e.printStackTrace(); } System.out.println("thread1 is going on ...."); System.out.println("thread1 is over!!!"); } } } private static class Thread2 implements Runnable{ @Override public void run(){ synchronized (TestD.class) { System.out.println("enter thread2...."); System.out.println("thread2 is sleep...."); //只有針對此對象調(diào)用notify()方法后本線程才進(jìn)入對象鎖定池準(zhǔn)備獲取對象鎖進(jìn)入運(yùn)行狀態(tài)。 TestD.class.notify(); //================== //區(qū)別 //如果我們把代碼:TestD.class.notify();給注釋掉,即TestD.class調(diào)用了wait()方法,但是沒有調(diào)用notify() //方法,則線程永遠(yuǎn)處于掛起狀態(tài)。 try { //sleep()方法導(dǎo)致了程序暫停執(zhí)行指定的時間,讓出cpu該其他線程, //但是他的監(jiān)控狀態(tài)依然保持者,當(dāng)指定的時間到了又會自動恢復(fù)運(yùn)行狀態(tài)。 //在調(diào)用sleep()方法的過程中,線程不會釋放對象鎖。 Thread.sleep(5000); } catch (Exception e) { e.printStackTrace(); } System.out.println("thread2 is going on...."); System.out.println("thread2 is over!!!"); } } } }
運(yùn)行效果:
enter thread1... thread1 is waiting... enter thread2.... thread2 is sleep.... thread2 is going on.... thread2 is over!!! thread1 is going on .... thread1 is over!!!
如果注釋掉代碼:
TestD.class.notify();
運(yùn)行效果:
enter thread1... thread1 is waiting... enter thread2.... thread2 is sleep.... thread2 is going on.... thread2 is over!!!
且程序一直處于掛起狀態(tài)。
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學(xué)習(xí)或者工作能帶來一定的幫助,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。
- java中sleep方法和wait方法的五個區(qū)別
- Java線程中sleep和wait的區(qū)別詳細(xì)介紹
- Java面試題篇之Sleep()方法與Wait()方法的區(qū)別詳解
- 詳解Java中wait和sleep的區(qū)別
- 詳解Java中的sleep()和wait()的區(qū)別
- Java中wait與sleep的區(qū)別講解(wait有參及無參區(qū)別)
- java sleep()和wait()的區(qū)別點(diǎn)總結(jié)
- Java詳細(xì)分析sleep和wait方法有哪些區(qū)別
- java面試突擊之sleep和wait有什么區(qū)別詳析
- Java中wait()與sleep()兩者的不同深入解析
相關(guān)文章
測試springboot項(xiàng)目出現(xiàn)Test Ignored的解決
這篇文章主要介紹了測試springboot項(xiàng)目出現(xiàn)Test Ignored的解決,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-11-11使用shardingsphere對SQLServer坑的解決
本文主要介紹了使用shardingsphere對SQLServer坑的解決,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-03-03SpringBoot 請求參數(shù)忽略大小寫的實(shí)例
這篇文章主要介紹了SpringBoot 請求參數(shù)忽略大小寫的實(shí)例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01Springboot使用thymeleaf動態(tài)模板實(shí)現(xiàn)刷新
這篇文章主要介紹了Springboot使用thymeleaf動態(tài)模板實(shí)現(xiàn)刷新,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-08-08如何使用Sentry 監(jiān)控你的Spring Boot應(yīng)用
這篇文章主要介紹了如何使用Sentry 監(jiān)控你的Spring Boot應(yīng)用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-11-11java中堆內(nèi)存與棧內(nèi)存的知識點(diǎn)總結(jié)
在本篇文章里小編給大家整理的是關(guān)于java中堆內(nèi)存與棧內(nèi)存的知識點(diǎn)總結(jié),有需要的朋友們可以跟著學(xué)習(xí)下。2019-12-12