<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不卡在线观看| 亚洲精品私拍国产福利在线| 亚洲狠狠成人综合网| 成年网站免费入口在线观看 | 免费黄色小视频网站| 亚洲精品tv久久久久| 亚洲人成在线观看| 外国成人网在线观看免费视频| 久久久久国色AV免费看图片 | 破了亲妺妺的处免费视频国产 | 国拍在线精品视频免费观看| 国产91在线免费| 无套内谢孕妇毛片免费看看| 亚洲免费中文字幕| 亚洲一级毛片中文字幕| 大妹子影视剧在线观看全集免费| 精品久久久久久久久免费影院| 久久久久久亚洲精品不卡| 亚洲一区无码中文字幕乱码| 久久久久女教师免费一区| 四虎成人精品一区二区免费网站 | 亚洲综合激情视频| 在线免费观看一级毛片| 亚洲视频网站在线观看| fc2成年免费共享视频网站| 精品国产免费观看久久久| 亚洲精彩视频在线观看| 国产免费人成视频尤勿视频 | 一级毛片免费毛片毛片| 午夜毛片不卡高清免费| 久久精品a亚洲国产v高清不卡| 日日摸夜夜添夜夜免费视频 | 亚洲欧洲中文日韩av乱码| 亚洲熟妇无码AV| 亚洲日韩在线中文字幕第一页| 亚洲国产高清国产拍精品| 国产人在线成免费视频| 久久亚洲精品成人av无码网站| 老司机精品免费视频|