Cyh的博客

          Email:kissyan4916@163.com
          posts - 26, comments - 19, trackbacks - 0, articles - 220

          導航

          公告

          一直努力努力努力,像奴隸奴隸奴隸!~~
          <2025年7月>
          293012345
          6789101112
          13141516171819
          20212223242526
          272829303112
          3456789

          常用鏈接

          隨筆檔案(25)

          文章分類(219)

          文章檔案(220)

          新聞檔案(66)

          相冊

          收藏夾(7)

          最新隨筆

          搜索

          •  

          最新評論

          閱讀排行榜

          線程4--線程的協作

          Posted on 2009-06-02 20:19 啥都寫點 閱讀(186) 評論(0)  編輯  收藏 所屬分類: J2SE
          關鍵技術:
          • 在synchronized代碼塊中使用wait方法,能夠使當前線程進入等待狀態,并釋放當前線程擁有的對象鎖。
          • 在synchronized代碼塊中使用notify或者notifyAll方法,當前線程釋放對象鎖,并喚醒其他正在等待該對象鎖的線程。當有多個線程都在等待該對象鎖時,由Java虛擬機決定被喚醒的等待線程。

          package book.thread;

          import java.util.Vector;

          /**
           * 線程間的協作
           
          */
          public class WaitNotify {
              
          /**
               * 打印信息的類,是一個線程。
               
          */
              
          static class Printer extends Thread{
                  Vector task 
          = new Vector();
                  
          boolean running = false;
                  
          public void start(){
                      
          this.running = true;
                      
          super.start();
                  }
                  
          public void run(){
                      
          try {
                          System.out.println(
          "Printer begin!");
                          
          while (running){
                              
          synchronized(this) {
                                  
          while ((task.size() == 0&& running){
                                      
          //如果任務列表為空,而且線程還允許運行,則等待任務
                                      System.out.println("wait begin!");
                                      
          //該線程進入等待狀態,直到被其他線程喚醒
                                      wait();
                                      System.out.println(
          "wait end!");
                                  }
                              }
                              
          if (running){
                                  System.out.println(
          "print the task: " + task.remove(0));
                              }
                          }
                          System.out.println(
          "Printer end!");
                      } 
          catch (InterruptedException e) {
                          e.printStackTrace();
                      }
                  }
                  
          /**
                   * 添加待打印的任務
                   
          */
                  
          public void addTask(String str){
                      
                      
          synchronized (this){
                          
          this.task.add(str);
                          
          //喚醒其他等待的線程
                          System.out.println("addTask notify!");
                          notify();
                          
          //notifyAll();
                      }
                  }
                  
          /**
                   * 停止線程
                   
          */
                  
          public void stopPrinter(){
                      
          this.running = false;
                      
          synchronized (this){
                          
          //喚醒其他等待的線程
                          System.out.println("stopPrinter notify!");
                          notify();
                      }
                  }
              }

              
          public static void main(String[] args) {
                  Printer printer 
          = new Printer();
                  
          //啟動打印線程
                  printer.start();
                  
          //添加任務
                  try {
                      Thread.sleep(
          200);
                      
          for (int i=0; i<5; i++){
                          
          //休眠200毫秒
                          Thread.sleep(200);
                          printer.addTask(
          "The task " + i);
                      }
                  }
                  
          catch (InterruptedException e) {
                      e.printStackTrace();
                  }
                  printer.stopPrinter();
              }
          }



                                                                                                                 --    學海無涯
                  

          主站蜘蛛池模板: 华坪县| 鄱阳县| 监利县| 仁怀市| 秭归县| 台南县| 泗水县| 双流县| 子长县| 高清| 东山县| 新干县| 辽宁省| 荔浦县| 信宜市| 邹平县| 东阳市| 抚顺县| 吴堡县| 龙川县| 安徽省| 连山| 白朗县| 溧水县| 错那县| 平湖市| 西城区| 叙永县| 邯郸县| 洪江市| 麦盖提县| 定南县| 忻城县| 汽车| 黄浦区| 闽清县| 禹州市| 广东省| 万州区| 濮阳县| 化州市|