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

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);
  }
 }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值