- 資訊首頁(yè) > 開(kāi)發(fā)技術(shù) > 編程語(yǔ)言 >
- java sleep()和wait()的區別點(diǎn)總結
wait()是Object的方法,sleep()是Thread的方法。
wait()必須采用同步方法,不需要sleep()方法。
線(xiàn)程在同步方法中執行sleep()方法,不釋放monitor鎖,wait()方法釋放monitor鎖。
短暫休眠后,sleep()方法會(huì )主動(dòng)退出阻塞,而wait()方法需要在沒(méi)有指定wait時(shí)間的情況下被其他線(xiàn)程中斷才能退出阻塞。
import java.text.SimpleDateFormat; import java.util.Date; public class TestSleepAndWait { public static void main(String[] args) { new Thread1().start(); try { Thread.sleep(100); } catch (InterruptedException e) { e.printStackTrace(); } new Thread2().start(); } } class Thread1 extends Thread{ private void sout(String s){ System.out.println(s+" "+new SimpleDateFormat("HH:mm:ss:SS").format(new Date())); } @Override public void run() { sout("enter Thread1.run"); synchronized (TestSleepAndWait.class){//wait只能在同步代碼塊或者同步方法中使用 sout("Thread1 is going to wait"); try { TestSleepAndWait.class.wait(); // 這里只能使用持有鎖TestSleepAndWait.class.wait(),使用其他對象則報錯java.lang.IllegalMonitorStateException } catch (InterruptedException e) { e.printStackTrace(); } sout("after waiting, thread1 is going on"); sout("thread1 is over"); } } } class Thread2 extends Thread{ private void sout(String s){ System.out.println(s+" "+new SimpleDateFormat("HH:mm:ss:SS").format(new Date())); } @Override public void run() { sout("enter Thread2.run"); synchronized (TestSleepAndWait.class){//wait只能在同步代碼塊或者同步方法中使用 sout("Thread2 is going to notify"); TestSleepAndWait.class.notify(); 這里只能使用持有鎖TestSleepAndWait.class sout("thread2 is going to sleep 10ms"); try { Thread.sleep(10); } catch (InterruptedException e) { e.printStackTrace(); } sout("after sleeping, thread2 is going on"); sout("thread2 is over"); } } }
內容擴展:
/** * */ package com.b510.test; /** * java中的sleep()和wait()的區別 * @author Hongten Java學(xué)習交流QQ群:589809992 我們一起學(xué)Java! * @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 { //調用wait()方法,線(xiàn)程會(huì )放棄對象鎖,進(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...."); //只有針對此對象調用notify()方法后本線(xiàn)程才進(jìn)入對象鎖定池準備獲取對象鎖進(jìn)入運行狀態(tài)。 TestD.class.notify(); //================== //區別 //如果我們把代碼:TestD.class.notify();給注釋掉,即TestD.class調用了wait()方法,但是沒(méi)有調用notify() //方法,則線(xiàn)程永遠處于掛起狀態(tài)。 try { //sleep()方法導致了程序暫停執行指定的時(shí)間,讓出cpu該其他線(xiàn)程, //但是他的監控狀態(tài)依然保持者,當指定的時(shí)間到了又會(huì )自動(dòng)恢復運行狀態(tài)。 //在調用sleep()方法的過(guò)程中,線(xiàn)程不會(huì )釋放對象鎖。 Thread.sleep(5000); } catch (Exception e) { e.printStackTrace(); } System.out.println("thread2 is going on...."); System.out.println("thread2 is over!!!"); } } } }
到此這篇關(guān)于java sleep()和wait()的區別點(diǎn)總結的文章就介紹到這了,更多相關(guān)java sleep()和wait()的區別內容請搜索腳本之家以前的文章或繼續瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
免責聲明:本站發(fā)布的內容(圖片、視頻和文字)以原創(chuàng )、來(lái)自本網(wǎng)站內容采集于網(wǎng)絡(luò )互聯(lián)網(wǎng)轉載等其它媒體和分享為主,內容觀(guān)點(diǎn)不代表本網(wǎng)站立場(chǎng),如侵犯了原作者的版權,請告知一經(jīng)查實(shí),將立刻刪除涉嫌侵權內容,聯(lián)系我們QQ:712375056,同時(shí)歡迎投稿傳遞力量。
Copyright ? 2009-2022 56dr.com. All Rights Reserved. 特網(wǎng)科技 特網(wǎng)云 版權所有 特網(wǎng)科技 粵ICP備16109289號
域名注冊服務(wù)機構:阿里云計算有限公司(萬(wàn)網(wǎng)) 域名服務(wù)機構:煙臺帝思普網(wǎng)絡(luò )科技有限公司(DNSPod) CDN服務(wù):阿里云計算有限公司 百度云 中國互聯(lián)網(wǎng)舉報中心 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證B2
建議您使用Chrome、Firefox、Edge、IE10及以上版本和360等主流瀏覽器瀏覽本網(wǎng)站