一段利用管道的多线程程序
2005-12-02 09:44
218 查看
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);
}
}
}
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);
}
}
}
相关文章推荐
- 利用管道技术实现将printf重定向到MFC对话框程序的编辑框控件中去
- 多线程程序一段问题代码分析
- [.net 面向对象程序设计进阶] (17) 多线程(Multithreading)(二) 利用多线程提高程序性能(中)
- VC中利用管道技术取得DOS或者命令行以及外部程序的执行结果
- 多线程程序一段问题代码分析(生产者消费者)
- 利用有名管道实现进程间通信(聊天小程序)
- 利用C#线程窗口调试多线程程序
- Linux多线程socket程序异常退出-断开的管道
- 1.利用有名管道和多线程实现双方互相通信
- 实现一个无锁的Stack,并写一段测试代码(多线程访问),证明这个Stack是线程安全的。给出程序以及运行的截图。
- python --利用Socket多线程的FTP程序
- 利用管道获取控制台程序的标准输出
- 多线程程序框架-利用互斥对象实现线程同步
- 利用Boost实现高度可移植的多线程C++程序
- 利用管道获取控制台程序的标准输出
- 利用多线程让没有窗口的程序显示进度条
- Android利用多线程提高程序性能
- 利用多线程实现linux下C语言的聊天室程序:
- 利用liunx的 crontab 和 php 的 pcntl_fork() 实现定时启动的多线程php程序
- [.net 面向对象程序设计进阶] (16) 多线程(Multithreading)(一) 利用多线程提高程序性能(上)