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

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 色香婷婷| 亚洲欧美成人综合 | 天堂资源8中文最新版 | 国产在线视频第一页 | 欧美视频黑鬼大战白妞 | 999精品视频这里只有精品 | 91嫩草国产在线观看免费 | 亚洲国产精品福利片在线观看 | 亚洲福利区 | 韩国办公室激情 | 亚洲琪琪 | 好大好硬好长好爽a网站 | ysl蜜桃色成人麻豆 youwu在线影院 | 国产成+人+综合+亚洲欧美丁香花 | 四虎成人www国产精品 | 免费观看伦理片 | 国产精品视频网 | 亚洲天堂男人天堂 | 脱女学小内内摸出水网站免费 | 国产特黄a级在线视频 | 小便japanesewctv| hh99me福利毛片| 国产香蕉国产精品偷在线观看 | 精品国内自产拍在线视频 | 国产成人影院一区二区 | 日本xx高清视频免费观看 | 北岛玲在线播放 | 亚洲性色永久网址 | 极品91| 国产麻豆精品视频 | 四虎成人永久地址 | ysl蜜桃色成人麻豆 youwu在线影院 | 国产精品怡红院在线观看 | 日本偷拍xxxxxxww | 猛男深夜狂cao小男生 | 日韩精品一二三区 | 国产91网站在线观看 | yy111111影院理论大片 | 国产aⅴ一区二区三区 | 四虎影院在线免费 | 好大用力深一点视频 |