首頁 > Java > java教程 > Java中關於sleep()與wait()的差異詳解

Java中關於sleep()與wait()的差異詳解

黄舟
發布: 2017-09-21 09:28:09
原創
1681 人瀏覽過

這篇文章主要介紹了詳解Java中的sleep()和wait()的區別的相關資料,希望透過本文能幫助到大家,需要的朋友可以參考下

詳解Java中的sleep()和wait()的區別

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

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

在呼叫sleep()方法的過程中,執行緒不會釋放物件鎖定。

而當呼叫wait()方法的時候,執行緒會放棄物件鎖,進入等待此物件的等待鎖定池,只有針對此物件呼叫notify()方法後本執行緒才進入物件鎖定池準備

取得物件鎖定進入運作狀態。

什麼意思呢?

舉個列子說明:


1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

/**

 *

 */

package com.b510.test;

 

/**

 * java中的sleep()和wait()的区别

 * @author Hongten Java学习交流QQ群:589809992 我们一起学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()方法,线程会放弃对象锁,进入等待此对象的等待锁定池

        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()方法后本线程才进入对象锁定池准备获取对象锁进入运行状态。

        TestD.class.notify();

        //==================

        //区别

        //如果我们把代码:TestD.class.notify();给注释掉,即TestD.class调用了wait()方法,但是没有调用notify()

        //方法,则线程永远处于挂起状态。

        try {

          //sleep()方法导致了程序暂停执行指定的时间,让出cpu该其他线程,

          //但是他的监控状态依然保持者,当指定的时间到了又会自动恢复运行状态。

          //在调用sleep()方法的过程中,线程不会释放对象锁。

          Thread.sleep(5000);

        } catch (Exception e) {

          e.printStackTrace();

        }

        System.out.println("thread2 is going on....");

        System.out.println("thread2 is over!!!");

      }

    }

  }

}

登入後複製

執行效果:


1

2

3

4

5

6

7

8

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

1 TestD.class.notify();

登入後複製

運行效果:


#

1

2

3

4

5

6

enter thread1...

thread1 is waiting...

enter thread2....

thread2 is sleep....

thread2 is going on....

thread2 is over!!!

登入後複製

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

以上是Java中關於sleep()與wait()的差異詳解的詳細內容。更多資訊請關注PHP中文網其他相關文章!

相關標籤:
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
最新問題
睡眠時間是否包含在執行時間限制中?
來自於 1970-01-01 08:00:00
0
0
0
javascript - 為什麼jQuery的remove不是同步的?
來自於 1970-01-01 08:00:00
0
0
0
為什麼省略 0ms sleep 會破壞我的 css 轉換?
來自於 1970-01-01 08:00:00
0
0
0
java - 多執行緒死鎖測試
來自於 1970-01-01 08:00:00
0
0
0
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板