一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - JAVA線程sleep()和wait()詳解及實例

JAVA線程sleep()和wait()詳解及實例

2020-09-29 14:08Java之家 Java教程

這篇文章主要介紹了JAVA線程sleep()和wait()詳解及實例的相關資料,探討一下sleep()和wait()方法的區別和實現機制,需要的朋友可以參考下

JAVA線程sleep()和wait()詳解及實例

sleep

1.sleep是Thread的一個靜態(static)方法。使得Runnable實現的線程也可以使用sleep方法。而且避免了線程之前相互調用sleep()方法,引發死鎖。

2.sleep()執行時需要賦予一個沉睡時間。在沉睡期間(阻塞線程期間),CPU會放棄這個線程,執行其他任務。當沉睡時間到了之后,該線程會自動蘇醒,不過此時線程不會立刻被執行,而是要等CPU分配資源,和其他線程進行競爭。

3.此外如果這個線程之前獲取了一個機鎖,在沉睡期間,這個機鎖不會釋放。其他等待這個機鎖的程序,必須等待這個線程醒來,且執行完后才能運行。

sleep相關代碼

?
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
public class ThreadTest2 {
 
  public static void main(String[] args){
    System.out.println("begin our test");
    ThreadSleep sleep = new ThreadSleep();
    try {
      Thread thread1 = new Thread(sleep,"路人甲");
      Thread thread2 = new Thread(sleep,"路人乙");
      thread1.start();
      thread2.start();
    }catch(Exception e){
      e.printStackTrace();
    }
    System.out.println("test is over");
  }
 
 
}
 
 class ThreadSleep implements Runnable{
 
   int count = 0;
 
   @Override
   public void run(){
     System.out.println(Thread.currentThread().getName() + " say : hello sleep !!");
     count();
 
   }
 
   public void count(){
     while(count < 20) {
         System.out.println(Thread.currentThread().getName() + " say : count is " + count);
         try {
           count++;
           Thread.sleep(100);
         } catch (Exception e) {
           e.printStackTrace();
         }
     }
 
   }
}

輸出日志

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
begin our test
test is over
路人甲 say : hello sleep !!
路人甲 say : count is 0
路人乙 say : hello sleep !!
路人乙 say : count is 1
路人甲 say : count is 2
路人乙 say : count is 2
路人甲 say : count is 4
路人乙 say : count is 4
路人甲 say : count is 6
路人乙 say : count is 7
路人乙 say : count is 8
路人甲 say : count is 8
路人甲 say : count is 10
路人乙 say : count is 10
路人乙 say : count is 12
路人甲 say : count is 12
路人乙 say : count is 14
路人甲 say : count is 14
路人甲 say : count is 16
路人乙 say : count is 16
路人甲 say : count is 18
路人乙 say : count is 18

通過日志可以發現線程甲和線程乙基本是交替執行,但是并不規律,且出現了并發問題。

該情況是由于代碼中設置了睡眠時間為100毫秒,由于count遞增執行速度很快,所以線程差不多是同時睡眠,然后同時蘇醒并導致了并發的出現。

接下來要添加synchronize塊,檢查sleep時機鎖是否釋放

?
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
public class ThreadTest2 {
 
  public static void main(String[] args){
    System.out.println("begin our test");
    ThreadSleep sleep = new ThreadSleep();
    try {
      Thread thread1 = new Thread(sleep,"路人甲");
      Thread thread2 = new Thread(sleep,"路人乙");
      thread1.start();
      thread2.start();
    }catch(Exception e){
      e.printStackTrace();
    }
    System.out.println("test is over");
  }
 
 
}
 
class ThreadSleep implements Runnable{
 
  int count = 0;
 
  @Override
  public void run(){
    System.out.println(Thread.currentThread().getName() + " say : hello sleep !!");
    count();
 
  }
 
  public void count(){
    while(count < 20) {
      synchronized (this) {
        System.out.println(Thread.currentThread().getName() + " say : count is " + count);
        try {
          count++;
          Thread.sleep(100);
        } catch (Exception e) {
          e.printStackTrace();
        }
 
      }
    }
 
  }
}

輸出日志

?
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
begin our test
路人甲 say : hello sleep !!
路人甲 say : count is 0
test is over
路人乙 say : hello sleep !!
路人甲 say : count is 1
路人甲 say : count is 2
路人甲 say : count is 3
路人甲 say : count is 4
路人甲 say : count is 5
路人甲 say : count is 6
路人甲 say : count is 7
路人甲 say : count is 8
路人甲 say : count is 9
路人甲 say : count is 10
路人甲 say : count is 11
路人甲 say : count is 12
路人甲 say : count is 13
路人甲 say : count is 14
路人甲 say : count is 15
路人甲 say : count is 16
路人甲 say : count is 17
路人甲 say : count is 18
路人甲 say : count is 19
路人乙 say : count is 20

通過日志可以看出,基本是線程甲在執行,這是因為sleep時,機鎖一直在線程甲上,所以線程乙只能一直等待直到線程甲釋放鎖。

wait

1.wait()是Object類的一個方法。當調用wait()方法時,該線程會進入和該對象相關的等待池中,并釋放它所擁有的機鎖。

2.執行wait()后,必須使用notify()方法或notifyAll()方法或設置等待時間(wait(long time))喚醒在等待線程池中的線程。

3.wait()必須放在synchronized block中,否則會在運行時報“java.lang.IllegalMonitorStateException”異常

wait相關代碼

?
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
public class ThreadTest2 {
 
  public static void main(String[] args) {
    System.out.println("begin our test");
    ThreadSleep sleep = new ThreadSleep();
    try {
      Thread thread1 = new Thread(sleep, "路人甲");
      Thread thread2 = new Thread(sleep, "路人乙");
      thread1.start();
      thread2.start();
    } catch (Exception e) {
      e.printStackTrace();
    }
    System.out.println("test is over");
  }
}
 
class ThreadSleep implements Runnable {
 
  int count = 0;
 
  @Override
  public void run() {
    System.out.println(Thread.currentThread().getName() + " say : hello sleep !!");
    count();
 
  }
 
  public void count() {
    while (count < 20) {
      synchronized (this) {
        System.out.println(Thread.currentThread().getName() + " say : count is " + count);
        try {
          count++;
          this.wait(100);
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
    }
 
  }
}

輸出日志

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
begin our test
路人甲 say : hello sleep !!
路人甲 say : count is 0
test is over
路人乙 say : hello sleep !!
路人乙 say : count is 1
路人甲 say : count is 2
路人乙 say : count is 3
路人甲 say : count is 4
路人乙 say : count is 5
路人甲 say : count is 6
路人乙 say : count is 7
路人甲 say : count is 8
路人乙 say : count is 9
路人甲 say : count is 10
路人乙 say : count is 11
路人甲 say : count is 12
路人乙 say : count is 13
路人乙 say : count is 14
路人甲 say : count is 15
路人乙 say : count is 16
路人甲 say : count is 17
路人乙 say : count is 18
路人甲 say : count is 19

通過日志可以發現在wait的情況下,機鎖會被釋放。

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

原文鏈接:https://my.oschina.net/u/241670/blog/842172

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产 日韩 欧美 综合 | 日本中文字幕黑人借宿影片 | 99热都是精品| 美女被无套进入 | 841995论坛网站2022年 | 93版高校教师 | 亚洲欧美久久一区二区 | 婷婷99视频精品全部在线观看 | 四虎影院精品在线观看 | 色吧五月婷婷 | 国产一级毛片国语版 | 国产一区二区免费在线 | 四虎影院久久久 | 大乳孕妇一级毛片 | 亚洲香蕉伊在人在线观婷婷 | 亚洲娇小性hd | 嫩模被黑人粗大挺进 | 人阁色第四影院在线观看 | 成年女人免费 | 色综合久久夜色精品国产 | 亚洲激情网| china中国xxxxfree| 亚洲天天综合 | 10个免费货源网站 | 久久精品国产色蜜蜜麻豆国语版 | a性片 | 久草在在线免视频在线观看 | 成人影院在线观看视频 | 7个黑人玩北条麻妃 | 亚洲精品久久久WWW游戏好玩 | 精品国产免费第一区二区三区日韩 | 亚洲精品国产国语 | 国产精品免费网站 | 好姑娘完整版在线观看中文 | 欧美日韩亚洲成人 | 亚洲第一在线播放 | 美女毛片在线 | 7788理论片在线观看 | 四大美女思春艳史片 | 国产精品久久免费 | 精品久久久久久久久免费影院 |