利用Piped实现线程间通讯

本文通过一个Java示例展示了如何使用PipedInputStream和PipedOutputStream实现两个线程之间的数据传递。发送线程不断输出数据,接收线程读取并打印接收到的数据。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 发送程序 :

   package com.thread;

import java.io.IOException;
import java.io.PipedOutputStream;

public class Thread1 extends Thread {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  new Thread1(new PipedOutputStream()).start();
 }
 
 PipedOutputStream pipeout;
 public Thread1(PipedOutputStream pipeout)
 {
  this.pipeout=pipeout;
 }
 public void run()
 {
  while (true)
  { 
   try {
    System.out.println(" THis is thread1");
    pipeout.write("ABCDEFGHIJKLMNOPQRSTUVWXYZ.".getBytes());
    pipeout.flush();
    System.out.println("发生数据");
    //pipeout.close();
    Thread.sleep(1000);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
  //pipeout.close();
 }

}
 接收线程:

package com.thread;

import java.io.IOException;
import java.io.PipedInputStream;

public class Thread2 implements Runnable {

 /**
  * @param args
  */
 public static void main(String[] args) {
  // TODO Auto-generated method stub
  new Thread(new Thread2(new PipedInputStream())).start();

 }

 PipedInputStream pipein;
 Thread2(PipedInputStream pipein)
 {
  this.pipein=pipein;
  
 }
 
 @Override
 public void run() {
  // TODO Auto-generated method stub
  while (true)
  { 
   try {
    System.out.println(" THis is thread2");
    byte[] bytes = new byte[10];
    int length = pipein.read(bytes);
    System.out.println("接收:"+new String(bytes,0,length));
    
    //Thread.sleep(10000);
   
    Thread.sleep(0);
   } catch (InterruptedException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }
 }

}
//主程序

package com.thread;

import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class Multi_Thread {

 /**
  * @param args
  */
 public static void main(String[] args) {
  
  
  
  
  try {
   PipedOutputStream pipeout= new PipedOutputStream();
   //方式一
   //PipedInputStream pipein= new PipedInputStream(pipeout);
   //方式二
   PipedInputStream pipein= new PipedInputStream();
   //方式二1
   //pipeout.connect(pipein);
   //方式二2   
   pipein.connect(pipeout);
   
   // TODO Auto-generated method stub
   Thread1 threadone=new Thread1( pipeout);
         //用Runnable接口类的对象创建线程;    
   Thread threadtwo=new Thread(new Thread2( pipein));
    threadone.start();
    threadtwo.start();//strat()方法启动线程;
    
   
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
  
 }

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值