輕松

          記述我學習java的里程

          常用鏈接

          統計

          積分與排名

          友情鏈接

          最新評論

          一段利用管道的多線程程序

          import java.io.*;

          public class PipeIODemo1{
           public static void main(String[] args) throws IOException{
            //創建一個管道輸出流對象
            PipedWriter out=new PipedWriter();
            
            //創建一個管道輸入流對象
            PipedReader in=new PipedReader();
            //把管道輸入流對象和管道輸出流對象聯接起來
            in.connect(out);
            
            //以上2個語句等效于
            //PipedReader in=new PipedReader(out);
            
                  OutThread objOut=new OutThread(out);
            InThread objIn=new InThread(in);
            objOut.start();
            objIn.start();

            try{
             objOut.join();
             objIn.join();
            }catch (InterruptedException e){}

                  System.out.println();
            System.out.println("Run Completed!!");
           }
          }


          //定義一個寫線程類
          class OutThread extends Thread{
           private Writer out;
           
           public OutThread(Writer out){
            this.out=out;
           }
           
           public void run(){
            try{
             try{
              for(char c='A'; c<='Z'; c++)
               out.write(c);
             }finally{
              out.close();
             }
            }catch(IOException e){
             getThreadGroup().uncaughtException(this, e);
            }
           }
          }

          class InThread extends Thread{
           private Reader in;

           public InThread(Reader in){
            this.in=in;
           }

           public void run(){
            int ch;
            try{
             try{
              while ((ch=in.read())!=-1)
               System.out.print((char)ch);
             }finally{
              in.close();
             }
            }catch(IOException e){
             getThreadGroup().uncaughtException(this, e);
            }
           }
          }

          posted on 2005-12-02 09:05 輕松 閱讀(770) 評論(0)  編輯  收藏 所屬分類: JAVA轉貼

          主站蜘蛛池模板: 广平县| 安徽省| 安塞县| 定安县| 修文县| 含山县| 宁河县| 襄城县| 安庆市| 兰溪市| 靖江市| 汝南县| 沽源县| 四平市| 岑巩县| 漳浦县| 龙陵县| 孟津县| 高台县| 兰考县| 宽城| 富顺县| 化德县| 忻城县| 普兰县| 浮山县| 克什克腾旗| 乐清市| 登封市| 南部县| 万山特区| 阿尔山市| 岱山县| 金华市| 阳泉市| 喀什市| 五指山市| 上蔡县| 苏尼特左旗| 凤山县| 新昌县|