java多線程通訊 例子

來源:互聯網
上載者:User

標籤:over   ring   nbsp   string   rac   wait   png   通訊   pack   

 

 

 1 package com.cl.www.thread; 2  3 public class NumberHolder { 4      5     private Integer number = 0; 6      7     // 增加number 8     public synchronized void increaseNum(){ 9         //不是0就不加10         while(number != 0){   11             try {12                 this.wait();13             } catch (InterruptedException e) {14                 e.printStackTrace();15             }16         }17         number++;18         System.out.println("number的值為:"+number);19         this.notifyAll(); //通知其它線程20     }21     22     //減少number23     public synchronized void decreaseNum(){24         //如果等於0就不減少25         while(number == 0){26             try {27                 this.wait();28             } catch (InterruptedException e) {29                 e.printStackTrace();30             }31         }32         number--;33         System.out.println("number的值為:"+number);34         this.notifyAll(); //通知其它線程35     }36 }

 

 

 1 package com.cl.www.thread; 2 /** 3  *  4  * 增加number 5  */ 6 public class IncreaseThread implements Runnable{ 7      8     private NumberHolder numberHolder; 9     10     public IncreaseThread(NumberHolder numberHolder) {11         this.numberHolder = numberHolder;12     }13 14     @Override15     public void run() {16         int i = 0;17         while(i<10){18             try {19                 Thread.sleep(1000);20             } catch (InterruptedException e) {21                 e.printStackTrace();22             }23             numberHolder.increaseNum();24             i++;25         }26     }27 }

 

 1 package com.cl.www.thread; 2  3 /** 4  * 減少number 5  * 6  */ 7 public class DecreaseThread implements Runnable{ 8      9     private NumberHolder numberHolder;10     11     public DecreaseThread(NumberHolder numberHolder) {12         13         this.numberHolder = numberHolder;14     }15     @Override16     public void run() {17         int i =0;18         while(i<10){19             try {20                 Thread.sleep((long)Math.random()*1000);21             } catch (InterruptedException e) {22                 e.printStackTrace();23             }24             numberHolder.decreaseNum();25             i++;26         }27     }28 }

 

 

 1 package com.cl.www.thread; 2  3 public class MainTest { 4     public static void main(String[] args) { 5          6         NumberHolder numberHolder = new NumberHolder(); 7          8         IncreaseThread increaseThread = new IncreaseThread(numberHolder); 9         DecreaseThread decreaseThread = new DecreaseThread(numberHolder);10         11         Thread th1 = new Thread(increaseThread);   // 加 number12         Thread th2 = new Thread(decreaseThread);   // 減 number13         14         Thread th3 = new Thread(increaseThread);    // 加 number15         Thread th4 = new Thread(decreaseThread);    // 減 number16         17         th1.start();18         th2.start();19         20         th3.start();21         th4.start();22     }23 }

 

輸出結果

java多線程通訊 例子

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.