<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    Dict.CN 在線詞典, 英語學習, 在線翻譯

    都市淘沙者

    荔枝FM Everyone can be host

    統計

    留言簿(23)

    積分與排名

    優秀學習網站

    友情連接

    閱讀排行榜

    評論排行榜

    Java 中利用管道實現線程間的通訊(轉)

    在Java 語言中,提供了各種各樣的輸入輸出流(stream),使我們能夠很方便的對數據進行操作,其中,管道(pipe)流是一種特殊的流,用于在不同線程(threads)間直接傳送數據。一個線程發送數據到輸出管道,另一個線程從輸入管道中讀數據。通過使用管道,實現不同線程間的通訊。無需求助于類似臨時文件之類的東西。本文在簡要介紹管道的基本概念后,將以一個具體的實例pipeapp加以詳細說明。
      1.管道的創建與使用
      Java提供了兩個特殊的專門的類專門用于處理管道,它們就是pipedinputstream類和pipeoutputstream類。
      Pipedinputstream代表了數據在管道中的輸出端,也就是線程向管道讀數據的一端;pipeoutputstream代表了數據在管道中的輸入端,也就是線程向管道寫數據的一端,這兩個類一起使用可以提供數據的管道流。
      為了創建一個管道流,我們必須首先創建一個pipeoutstream對象,然后,創建pipeinputstream對象,實例如下:
      pipeout= new pipedyoutstream();
      pipein= new pipedputsteam(pipepout);
      一旦創建了一個管道后,就可以象操作文件一樣對管道進行數據的讀寫。
      2.演示程序: pipeapp
      應用程序由三個程序組成:主線程(pipeapp.Java)及由主線程啟動的兩個二級線程(ythread.Java和zthread.Java),它們使用管道來處理數據。程序從一個內容為一行一行"x"字母的"input.txt"文件中讀取數據,使用管道傳輸數據,第一次是利用線程ythread將數據"x"轉換為"y",最后利用線程zthread將"y"轉換為"z",之后,程序在屏幕上顯示修改后的數據。
      主線程 (pipeapp.Java)
      在main()方法中,程序首先創建一個應用對象:pipeapp pipeapp=new pipeapp();
      由于程序中流操作都需要使用IOException異常處理,所以設置了一個try塊。在try中,為了從源文件中讀取數據,程序為"input.txt"文件創建了一個輸入流Xfileln,:
      fileinputstream xfileln= new fileinputstream("input.txt");
      新的輸入流傳遞給changetoy()方法,讓線程ythread能讀取該文件:
      inputstream ylnpipe =pipeapp.changetoy(xfileln);
      changetoy()方法創建將輸入數據"x"改變到"y"的線程ythread,并返回該線程的輸入管道:
      inputstream zlnpipe = pipeapp.changetoz(ylnpipe);
      changetoz()方法啟動將數據從"y"改變到"z"的線程zehread,主程序將使用從changetoz()返回的輸入管道。得到以修改的數據。
      然后,程序將管道輸入流定位到datainputstream對象,使程序能夠使用readline()方法讀取數據:
        datainputstream inputstream = new datainputstream(zlnpiepe);
      創建了輸入流以后,程序就可以以行一行的讀取數據病顯示在屏幕上。
      String str= inputstream.readline();
        While(str!=null)
        {
      system.out.println(str);   str=inputstream.readline();
        }
      顯示完成之后,程序關閉輸入流:
      inputstream.close();
      changetoy()方法
      changetoy()方法首先通過傳遞一個參數inputstream給datainputstream對象來定位資源的輸入流,使程序能使用readline()方法從流中讀取數據:
      datainputstream xfileln =new datainutstream(inputstream);
      然后,changetoy()創建輸出管道和輸入管道:
      pipeoutstream pipeout = new pipeoutputstream();
      pipeinputstream pipeln = new pipedinputsteam(pipeout);
      為了能夠使用println()方法輸出修改的后的文本行到管道,程序將輸出管道定位到printstream對象:
      printstream printstream = new printstream(pipeout);
      現在,程序可以創建將數據從x改變到y的線程,該線程是ythread類的一個對象,他傳遞兩個參數:輸入文件(xfileln)和輸出管道(調用printstream)
       ythread ythread =new thread(xfileln,printstream);
      之后,程序啟動線程:
      changetoz()方法
      changetoz()方法與changetoy()方法很相似,他從changetoy()返回的輸入流開始:
      datainputstream yfileln= new datainputstream(inputstream);
      程序創建一個新的管道:
      pipedoutstream pipeout2 = new pipedoutputstream();
        pipedinputstream pipeln2 = new pipedinputsream(pipeout2);
      該線程通過這個新的管道發出修改后的數據(輸入流pipeln2)給主程序。
      源程序如下:
      //
      //pipeapp.Java-pipeapp的主應用程序
      //
      import Java.io.*
      class pipeapp
      {
      public static void main(string[] args)
      {
      pipeapp pipeapp=new pipeapp();
      try
      {
      fileinputstream xfile =new fileinputstream("input.txt");
      inputstream ylnpipe = pipeapp.changetoy(xfileln);
      inputstream zlnpipe=pipeapp.changetoz(ylnpipe);
      system.out.println();
      system.out.println("here are the results");
      system.out.pringln();
      datainputstream inputstream = nes datainputstream(zlnpipe);
      string str = inputstream.readline();
      while (str!=null)
      {
      system.out.println(str);
      str=inputstream.readline();
      }
      inputstream.close();
      }
      catch(exception e)
      {
      system.out.println(e.tostring());
      }
      }
      public inputstream changetoy(inputstream inputstream)
      {
      try
      {
      datainputstream pipeout = new datainputsteam(inputstream);
      pipedoutstream pipeout = new pipedoutputstream();
      pipedlnsteam pipeln = new pipedlnputstream(pipeout);
      printstream printstream = new printstream(pipeout);
      ythread ythread = new ythread(xfileln,printstream);
      ythread.start();
      return pipeln;
      }
      catch(exeption e)
      {
      system.out.println(x.tostring());
      }
      return null;
      }
      public inputstream changetoz(inputstream inputsteam)
      {
      try
      {
      datainputstream yfileln = new datainputstream(inputstream);
      pipeoutputstream pipeln2 = new pipedinputstream(pipeout2);
      printrstream printstream2 = new printsteam(pipeout2);
      zthread zthread = new zthread(yfileln,printstream2);
      zthread.start();
      return pipeln2;
      }
      catch(exception e)
      {
      system.out.println(e.tostring());
      }
      return null;
      }
      }
      Ythread類和Zthread類
      由于ythread類與zthread類基本一樣,在此僅以ythread為例加以說明。
      Ythread的構造器接收兩個參數:輸入的文件和第一個管道的輸出端,構造器存儲這兩個參數作為類的數據成員:
       Ythread(datainputstream xfileln,pringstream printstream)
        {
      this.xfileln = xfileln;
      this.printstream = printstream;
        }
      線程通過run()方法來處理數據。首先讀取一行數據,確保xstring不為空的情況下循環執行:
      string xstring = xfileln.readline();
      每讀一行數據,完成一次轉換
      string ystring = xstring.replace('x','y');
      然后將修改后的數據輸出到管道的輸出端:
      prinstream.prinrln(ystring);
      為了確保所有緩沖區的數據完全進入管道的輸出端:
      pringstram.flush();
      循環完成后,線程關閉管道輸出流:
       pringstram.close();
      ythread類的源程序如下:
      //ythread.Java
      //
      import Java.io.*;
      class ythread exteads thread
      {
      datainputstream xfileln;
      pringstream printstream;
      ythread(datainputstream xfileln,pringstream.printstream)
      {
      this.xfileln = xfileln;
      this.printstream = printstream;
      }
      public void run()
      {
      try
      {
      string xstring = xfileln.readline();
      while(xstring!=null)
      {
      string ystring= xstring.replace('x','y');
      printstream.pringln(ystring);
      printstream.flush();
      xstring= xfileln.readline();
      }
      printstream.close();
      }
      catch{ioexception e}
      {
      system.out.println(e.tostring());
      }
      }
      }

    posted on 2007-09-18 22:25 都市淘沙者 閱讀(609) 評論(0)  編輯  收藏 所屬分類: Java Basic/Lucene/開源資料

    主站蜘蛛池模板: 永久免费av无码不卡在线观看| 最新亚洲成av人免费看| 亚洲一本综合久久| 免费人成动漫在线播放r18 | 91精品免费观看| 久久久久无码专区亚洲av| 亚洲日本va一区二区三区| 91青青国产在线观看免费| 国产亚洲一区区二区在线| 老牛精品亚洲成av人片| 在线观看免费高清视频| 亚洲黄色网站视频| 免费萌白酱国产一区二区三区 | 国产AV无码专区亚洲AV麻豆丫| 久草免费在线观看视频| 久久亚洲成a人片| fc2成年免费共享视频18| 国产高清视频在线免费观看| 精品日韩99亚洲的在线发布| 99re热精品视频国产免费| 国产AV无码专区亚洲AV男同| 九九久久精品国产免费看小说| 永久在线毛片免费观看| 456亚洲人成在线播放网站| 91短视频在线免费观看| 亚洲av无码成人黄网站在线观看| 国产精品1024在线永久免费 | 国产精品亚洲产品一区二区三区 | 91香蕉国产线在线观看免费| 亚洲精品无码成人AAA片| 国产V片在线播放免费无码| 五月天婷亚洲天综合网精品偷| 亚洲国产成人精品无码区二本| 24小时免费直播在线观看| 亚洲免费闲人蜜桃| 免费观看激色视频网站bd | 亚洲AV成人影视在线观看| **实干一级毛片aa免费| 久久精品国产亚洲AV麻豆网站| 久久久免费的精品| 亚洲日本中文字幕区|