java中的管道流(pipeStream)是一种特殊的流,用于在不同线程间直接传送数据。一个线程发送数据到输出管道,另外一个线程从输入管道中读取数据。通过使用管道,实现不同线程间的通信,而不必借助类似临时文件之类的东西。jdk提供4个类来使线程建可以进行通信。

(1)PipedInputStream与PipedOutputStream

(2)PipedReader与PipedWriter

(1)PipedInputStream与PipedOutputStream

package com.ming.thread.pipeinputoutput;

import java.io.PipedInputStream;

public class ReadData {

    public void readMethod(PipedInputStream input){
try {
System.out.println("read :");
byte[] byteArray=new byte[20];
int readLength=input.read(byteArray);
while(readLength!=-1){
String newData=new String(byteArray,0,readLength);
System.out.print(newData);
readLength=input.read(byteArray);
}
System.out.println();
input.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
package com.ming.thread.pipeinputoutput;

import java.io.PipedOutputStream;

public class WriteData {

    public void writeMethod(PipedOutputStream out){
try {
System.out.println("write :");
for(int i=0;i<300;i++){
String outData="" +(i+1);
out.write(outData.getBytes());
System.out.print(outData);
}
System.out.println();
out.close();
} catch (Exception e) {
// TODO: handle exception
}
}
}
package com.ming.thread.pipeinputoutput;

import java.io.PipedOutputStream;

public class ThreadWrite extends Thread {

    WriteData write;

    PipedOutputStream out;

    public ThreadWrite(WriteData write,PipedOutputStream out){
this.write=write;
this.out=out;
} public void run(){
write.writeMethod(out);
}
}
package com.ming.thread.pipeinputoutput;

import java.io.PipedInputStream;

public class ThreadRead extends Thread {

    ReadData read;

    PipedInputStream input;

    public ThreadRead(ReadData read,PipedInputStream input){
this.read=read;
this.input=input;
} public void run(){
read.readMethod(input);
}
}
package com.ming.thread.pipeinputoutput;

import java.io.PipedInputStream;
import java.io.PipedOutputStream; /**
* 管道流的一个测试
* @author mingge
*
*/
public class Run { public static void main(String[] args) {
try {
WriteData writeData=new WriteData();
ReadData readData=new ReadData();
PipedInputStream input=new PipedInputStream();
PipedOutputStream out=new PipedOutputStream();
out.connect(input);
ThreadRead threadRead=new ThreadRead(readData, input);
threadRead.start();
Thread.sleep(2000);
ThreadWrite threadWrite=new ThreadWrite(writeData, out);
threadWrite.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}

(2)PipedReader与PipedWriter

package com.ming.thread.pipereaderwriter;

import java.io.PipedReader;

public class ReadData {

    public void readMethod(PipedReader read){
try {
System.out.println("read :");
char[] byteArray=new char[20];
int readLength=read.read(byteArray);
while(readLength!=-1){
String newData=new String(byteArray, 0, readLength);
System.out.print(newData);
readLength=read.read(byteArray);
}
System.out.println();
read.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.ming.thread.pipereaderwriter;

import java.io.PipedWriter;

public class WriteData {

    public void writeMethod(PipedWriter out){
try {
System.out.println("write :");
for(int i=0;i<300;i++){
String outData=""+(i+1);
out.write(outData);
System.out.print(outData);
}
System.out.println();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
package com.ming.thread.pipereaderwriter;

import java.io.PipedReader;

public class ThreadReader extends Thread {

    ReadData read;

    PipedReader input;

    public ThreadReader(ReadData read,PipedReader input){
this.read=read;
this.input=input;
} public void run(){
read.readMethod(input);
}
}
package com.ming.thread.pipereaderwriter;

import java.io.PipedWriter;

public class ThreadWrite extends Thread {

    WriteData write;

    PipedWriter out;

    public ThreadWrite(WriteData write,PipedWriter out){
this.write=write;
this.out=out;
} public void run(){
write.writeMethod(out);
}
}
package com.ming.thread.pipereaderwriter;

import java.io.PipedReader;
import java.io.PipedWriter; public class Run { public static void main(String[] args) {
try {
WriteData writeData=new WriteData();
ReadData readData=new ReadData(); PipedReader inputStream=new PipedReader();
PipedWriter outputStream=new PipedWriter();
outputStream.connect(inputStream); ThreadReader threadRead=new ThreadReader(readData, inputStream);
threadRead.start(); Thread.sleep(2000); ThreadWrite threadWrite=new ThreadWrite(writeData, outputStream);
threadWrite.start(); } catch (Exception e) {
e.printStackTrace();
}
}
}

就是看代码就可以了...

java多线程通过管道流实现不同线程之间的通信的更多相关文章

  1. java多线程之管道流

    java语言中提供了各种各样的流供我们操纵数据,其中管道流(pipeStream)是一种特殊的流,用于在不同线程间直接传送数据. 一个线程发送数据到输出管道,另一个线程从输入管道读取数据,通过使用管道 ...

  2. 应用java多线程实现server端与多client之间的通信

    应用多线程来实现server与多线程之间的通信的基本步骤 1.server端创建ServerSocket,循环调用accept()等待client链接 2.client创建一个Socket并请求和se ...

  3. JAVA多线程提高四:多个线程之间共享数据的方式

    多个线程访问共享对象和数据的方式 如果每个线程执行的代码相同,可以使用同一个Runnable对象,这个Runnable对象中有那个共享数据,例如,买票系统就可以这么做. 如果每个线程执行的代码不同,这 ...

  4. Java多线程编程-线程之间的通信

    转载自:这里 学习了基础的线程知识 看到了 线程之间的通信 线程之间有哪些通信方式呢? 1.同步 这里讲的同步是指多个线程通过synchronized关键字这种方式来实现线程间的通信. public ...

  5. Java中的管道流 PipedOutputStream和PipedInputStream

    我们在学习IO流的时候可能会学字节流.字符流等,但是关于管道流的相信大部分视频或者教程都是一语带过,第一个是因为这个东西在实际开发中用的也不是很多,但是学习无止境,存在既有理.JDK中既然有个类那说明 ...

  6. 系统学习 Java IO (六)----管道流 PipedInputStream/PipedOutputStream

    目录:系统学习 Java IO---- 目录,概览 PipedInputStream 类使得可以作为字节流读取管道的内容. 管道是同一 JVM 内的线程之间的通信通道. 使用两个已连接的管道流时,要为 ...

  7. Java多线程编程核心技术-第3章-线程间通信-读书笔记

    第 3 章 线程间通信 线程是操作系统中独立的个体,但这些个体如果不经过特殊的处理就不能成为一个整体.线程间的通信就是成为整体的必用方案之一,可以说,使线程间进行通信后,系统之间的交互性会更强大,在大 ...

  8. Java多线程系列--“基础篇”06之 线程让步

    概要 本章,会对Thread中的线程让步方法yield()进行介绍.涉及到的内容包括:1. yield()介绍2. yield()示例3. yield() 与 wait()的比较 转载请注明出处:ht ...

  9. Java多线程系列--“基础篇”07之 线程休眠

    概要 本章,会对Thread中sleep()方法进行介绍.涉及到的内容包括:1. sleep()介绍2. sleep()示例3. sleep() 与 wait()的比较 转载请注明出处:http:// ...

随机推荐

  1. WPARAM和LPARAM的含义

    lParam 和 wParam 是宏定义,一般在消息函数中带这两个类型的参数,通常用来存储窗口消息的参数. LRESULT CALLBACK WindowProc(HWND hwnd, UINT uM ...

  2. C#中的多线程 - 高级多线程

    1非阻塞同步Permalink 之前,我们描述了即使是很简单的赋值或更新一个字段也需要同步.尽管锁总能满足这个需求,一个存在竞争的锁意味着肯定有线程会被阻塞,就会导致由上下文切换和调度的延迟带来的开销 ...

  3. 从底层了解ASP.NET体系结构

    导读:  前言  关于ASP.NET的底层的工作机制,最近园子里讨论的甚是火热.相信很多人都看过Rick Strahl先生的一篇经典之作:A low-level Look at the ASP.NET ...

  4. Python的__getattr__方法学习

    内容部分来自网络 __getattr__函数的作用: 如果属性查找(attribute lookup)在实例以及对应的类中(通过__dict__)失败, 那么会调用到类的__getattr__函数: ...

  5. 自定义等高 Cell

    1.介绍 1.1 代码自定义 cell(使用 frame) 创建一个继承自 UITableViewCell 的子类,比如 BookCell1. 在 initWithStyle:reuseIdentif ...

  6. linux影响上传文件大小的因素

    主要是从三个方面 ①.php代码方面(这个无需说明) ②.php.ini配置 max_execution_time = 600 ;每个PHP页面运行的最大时间值(秒),默认30秒max_input_t ...

  7. [Study notes] To programing RGBD-SLAM together from Gaoxiang

    Solve CMake Error in CMakeLists.txt (FIND_PAKAGE): CMake Error at src/CMakeLists.txt:5 (FIND_PACKAGE ...

  8. idea 面板介绍

    一.面板说明 IDEA面板的全貌如下图 二.菜单栏 下面会简单介绍下一些常用的部分菜单使用,如有疑问或补充欢迎留言. (1).File文件 1. New:新建一个工程 可以新建project,导入已存 ...

  9. 阿里云linux安装jmeter并进行压测

    一.阿里云linux安装JDK 1.下载安装JDK jdk官网,选择linux版本,下载并保存. (一)yum安装 安装epel的yumyuan yum install epel-release -y ...

  10. SQLException: Incorrect string value: '\xE4\xB8\xAD\xE5\x9B\xBD' for column at row 1

    这个是由于新建数据库没有选择默认字符集导致的,只要选择utf8即可. 如果以上还无法解决,那可能是表里的varchar字符集也不对