您的位置:首頁技術文章
文章詳情頁

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

瀏覽:3日期:2024-07-01 10:24:16
內容: 在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());}}} 出處 jianada··emook Java, java, J2SE, j2se, J2EE, j2ee, J2ME, j2me, ejb, ejb3, JBOSS, jboss, spring, hibernate, jdo, struts, webwork, ajax, AJAX, mysql, MySQL, Oracle, Weblogic, Websphere, scjp, scjd
標簽: Java
相關文章:
国产综合久久一区二区三区