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