JAVA sleep() & wait()

來源:互聯網
上載者:User

標籤:技術分享   ...   分配   佔用   tool   main   ble   star   過程   

對於sleep()方法,我們首先要知道該方法是屬於Thread類中的。而wait()方法,則是屬於Object類中的。

sleep()方法導致了程式暫停執行指定的時間,讓出cpu該其他線程,但是他的監控狀態依然保持者,當指定的時間到了又會自動回復運行狀態。

在調用sleep()方法的過程中,線程不會釋放對象鎖。

而當調用wait()方法的時候,線程會放棄對象鎖,進入等待此對象的等待鎖定池,只有針對此對象調用notify()方法後本線程才進入對象鎖定池準備

擷取對象鎖進入運行狀態。

什麼意思呢?

舉個列子說明:

 1 /** 2  *  3  */ 4 package com.b510.test; 5  6 /** 7  * java中的sleep()和wait()的區別 8  * @author Hongten 9  * @date 2013-12-1010  */11 public class TestD {12 13     public static void main(String[] args) {14         new Thread(new Thread1()).start();15         try {16             Thread.sleep(5000);17         } catch (Exception e) {18             e.printStackTrace();19         }20         new Thread(new Thread2()).start();21     }22     23     private static class Thread1 implements Runnable{24         @Override25         public void run(){26             synchronized (TestD.class) {27             System.out.println("enter thread1...");    28             System.out.println("thread1 is waiting...");29             try {30                 //調用wait()方法,線程會放棄對象鎖,進入等待此對象的等待鎖定池31                 TestD.class.wait();32             } catch (Exception e) {33                 e.printStackTrace();34             }35             System.out.println("thread1 is going on ....");36             System.out.println("thread1 is over!!!");37             }38         }39     }40     41     private static class Thread2 implements Runnable{42         @Override43         public void run(){44             synchronized (TestD.class) {45                 System.out.println("enter thread2....");46                 System.out.println("thread2 is sleep....");47                 //只有針對此對象調用notify()方法後本線程才進入對象鎖定池準備擷取對象鎖進入運行狀態。48                 TestD.class.notify();49                 //==================50                 //區別51                 //如果我們把代碼:TestD.class.notify();給注釋掉,即TestD.class調用了wait()方法,但是沒有調用notify()52                 //方法,則線程永遠處於掛起狀態。53                 try {54                     //sleep()方法導致了程式暫停執行指定的時間,讓出cpu該其他線程,55                     //但是他的監控狀態依然保持者,當指定的時間到了又會自動回復運行狀態。56                     //在調用sleep()方法的過程中,線程不會釋放對象鎖。57                     Thread.sleep(5000);58                 } catch (Exception e) {59                     e.printStackTrace();60                 }61                 System.out.println("thread2 is going on....");62                 System.out.println("thread2 is over!!!");63             }64         }65     }66 }

運行效果:

enter thread1...thread1 is waiting...enter thread2....thread2 is sleep....thread2 is going on....thread2 is over!!!thread1 is going on ....thread1 is over!!!

如果注釋掉代碼:

1 TestD.class.notify();

運行效果:

enter thread1...thread1 is waiting...enter thread2....thread2 is sleep....thread2 is going on....thread2 is over!!!

且程式一直處於掛起狀態。

① 這兩個方法來自不同的類分別是,sleep來自Thread類,和wait來自Object類。

sleep是Thread的靜態類方法,誰調用的誰去睡覺,即使在a線程裡調用b的sleep方法,實際上還是a去睡覺,要讓b線程睡覺要在b的代碼中調用sleep。

 

② 鎖: 最主要是sleep方法沒有釋放鎖,而wait方法釋放了鎖,使得其他線程可以使用同步控制塊或者方法。

sleep不出讓系統資源;wait是進入線程等待池等待,出讓系統資源,其他線程可以佔用CPU。一般wait不會加時間限制,因為如果wait線程的運行資源不夠,再出來也沒用,要等待其他線程調用notify/notifyAll喚醒等待池中的所有線程,才會進入就緒隊列等待OS分配系統資源。sleep(milliseconds)可以用時間指定使它自動喚醒過來,如果時間不到只能調用interrupt()強行打斷。

Thread.sleep(0)的作用是“觸發作業系統立刻重新進行一次CPU競爭”。

③ 使用範圍:wait,notify和notifyAll只能在同步控制方法或者同步控制塊裡面使用,而sleep可以在任何地方使用。

   synchronized(x){ 
      x.notify() 
     //或者wait() 
   }

JAVA sleep() & wait()

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.