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

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

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

服務器之家 - 編程語言 - Java教程 - Java多線程通訊之wait,notify的區別詳解

Java多線程通訊之wait,notify的區別詳解

2021-05-20 13:53LoseMyFuture Java教程

這篇文章主要介紹了Java多線程通訊之wait,notify的區別詳解,非常不錯,具有一定的參考借鑒借鑒價值,需要的朋友可以參考下

下面通過代碼給大家介紹java多線程通訊之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
class res{
 public string username;
 public string sex;
}
class out extends thread{
 res res;
 public out(res res){
  this.res=res;
 }
 @override
 public void run() {
  //寫操作
  int count=0;
  while (true){
//   synchronized (res.getclass()){
    if(count==0){//偶數
     res.username="小明";
     res.sex="男";
    } else {//奇數
     res.username="小紅";
     res.sex="女";
    }
    count=(count+1)%2;
//   }
   }
 }
}
class input extends thread{
 res res;
 public input(res res){
  this.res=res;
 }
 @override
 public void run() {
  while (true){
//   synchronized (res.getclass()){
    system.out.println(res.username+","+res.sex);
//   }
  }
 }
}
public class outinputthread {
 public static void main(string[] args) {
  res res = new res();
  out out = new out(res);
  input input = new input(res);
  out.start();
  input.start();
 }
}
小紅,女
小紅,女
小紅,女
小紅,女
小紅,女
小紅,女
小紅,女
小紅,女
小紅,女

出現以上結果??消費者一直消費或者生產者一直生產

解決方法:生產者生產完成后消費者方可消費,否者不可消費,消費者未消費或者未消費完生產者不可生產,一次生產一次消費。其實也就是保證對res共享資源的操作同一時刻僅有同一個線程進行操作,

wait、notify、notifyall方法

wait、notify、notifyall是三個定義在object類里的方法,可以用來控制線程的狀態。

這三個方法最終調用的都是jvm級的native方法。隨著jvm運行平臺的不同可能有些許差異。

 如果對象調用了wait方法就會使持有該對象的線程把該對象的控制權交出去,然后處于等待狀態。當前線程從運行變為阻塞,釋放所的資源

如果對象調用了notify方法就會通知某個正在等待這個對象的控制權的線程可以繼續運行。讓持有該鎖的線程從阻塞態變為就緒。

如果對象調用了notifyall方法就會通知所有等待這個對象控制權的線程繼續運行。

注意:一定要在線程同步中使用,并且是同一個鎖的資源

通過以下方式即可完成需求。

生產者獲取res.getclass鎖后,如果flag為true生產者通過調用res.getclass.wait進行等待,此時其他線程可獲取該鎖,如果flag為false,進行生產,然后設置flag為true保證資源消費后方可再生產,接著通過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
67
68
69
70
71
72
73
class res{
 public string username;
 public string sex;
 //true 生產者等待,消費者可消費 false生產者可以生產,消費者不可消費
 public boolean flag=false;
}
class out extends thread{
 res res;
 
 public out(res res){
  this.res=res;
 }
 @override
 public void run() {
  //寫操作
  int count=0;
  while (true){
   synchronized (res.getclass()){
    if(res.flag){
     try {
      res.getclass().wait();//讓當前線程從運行變為阻塞,并且釋放所的資源
     } catch (interruptedexception e) {
      e.printstacktrace();
     }
    }
    if(count==0){//偶數
     res.username="小明";
     res.sex="男";
    } else {//奇數
     res.username="小紅";
     res.sex="女";
    }
    count=(count+1)%2;
    res.flag=true;
    res.getclass().notify();
   }
   }
 
 }
}
class input extends thread{
 res res;
 public input(res res){
  this.res=res;
 }
 
 @override
 public void run() {
  while (true){
   synchronized (res.getclass()){
    if(!res.flag){
     try {
      res.getclass().wait();
     } catch (interruptedexception e) {
      e.printstacktrace();
     }
    }
    system.out.println(res.username+","+res.sex);
    res.flag=false;
    res.getclass().notify();
   }
  }
 }
}
public class outinputthread {
 public static void main(string[] args) {
  res res = new res();
  out out = new out(res);
  input input = new input(res);
  out.start();
  input.start();
 }
}

輸出如下:

小明,男
小紅,女
小明,男
小紅,女
小明,男
小紅,女
小明,男
小紅,女

如果去掉notify會怎樣?去掉一個?去掉兩個?

去掉一個生產者可以打印多個(但是也不多),去掉消費者僅可打印一個,去掉兩個可能不打印,也可能打印1個,所以wait、notify必須成對使用

wait(用于同步中)與sleep區別?

都是做休眠,wait需要notify

對于sleep方法,我們首先要知道該方法是屬于thread類中的。而wait方法,則是屬于object類中的。

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

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

而當調用wait方法的時候,線程會放棄對象鎖,進入等待此對象的等待鎖定池,只有針對此對象調用notify方法后本線程才進入對象鎖定池準備獲取對象鎖進入運行狀態。

總結

以上所述是小編給大家介紹的java多線程通訊之wait,notify的區別詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!

原文鏈接:https://www.cnblogs.com/losemyfuture/archive/2018/07/24/9357846.html

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 欧美夫妇野外交换hd高清版 | 精品视频入口 | eee在线播放成人免费 | 香港三级系列在线播放 | 欧美香蕉 | 日本videohd18| 国产精品原创永久在线观看 | 好妈妈7在线观看高清 | 国产精品久久久久久久久ktv | 欧美特级午夜一区二区三区 | 大学生情侣在线 | 痴mu动漫成年动漫在线观看 | 97久久精品午夜一区二区 | 国产精品1区2区 | 久久99r66热这里有精品 | 四虎影院在线免费观看 | 日韩精品特黄毛片免费看 | 丁香六月色婷婷综合网 | 99re5精品视频在线观看 | 欧美视频在线播放观看免费福利资源 | 九九九九在线精品免费视频 | 操岳母娘| 国产精品1 | 亚洲人成在线观看一区二区 | bl双性肉文| 强行扒开美女大腿挺进 | 四虎影视在线影院在线观看观看 | 精品国产品在线18年 | 日韩国产欧美精品综合二区 | 欧美性f | 99热视| 和老外3p爽粗大免费视频 | 91啪在线观看国产在线 | 夫妻性生活在线 | 亚洲欧美综合区自拍另类 | 久久亚洲精品专区蓝色区 | 免费高清在线视频色yeye | 俺去也亚洲色图 | 青青草精品在线观看 | 激情六月丁香婷婷四房播 | 亚洲 欧美 国产 在线 日韩 |