`
byandby
  • 浏览: 1696070 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

管道输入流 PipedInputStream

阅读更多
     管道输入流从一个管道输出流中读取数据。通常由一个线程向管道输出流写数据,由另一个线程从管道输入流中读取数据,两个线程可以用管道来通信。当线程A执行管道输入流的的read()方法时,如果暂时还没有数据,这个线程就会被阻塞,只有当线程B向管道输出流写了新的数据后,线程A才会恢复运行。 下面我们来演示一个例子

Sender类
package xiaohang.com;

import java.io.PipedOutputStream;

/** 向管道输出流写数据的线程 */
class Sender extends Thread {
	private PipedOutputStream out = new PipedOutputStream();

	public PipedOutputStream getPipedOutputStream() {
		return out;
	}

	@Override
	public void run() {
		try {
			for (int i = -127; i < 128; i++) {
				out.write(i);
				// 暂停当前正在执行的线程执行其它线程
				yield();
			}
			// 关闭此管道输出流并释放与此流有关的所有系统资源 此流可能无法再用于写入字节。
			out.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}
}


  Receiver类
package xiaohang.com;

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

/** 从管道输入流读取数据的线程 */
public class Receiver extends Thread {
	private PipedInputStream in;

	public Receiver(Sender sender) throws IOException {
		// 使此管道输入流连接到管道输出流 PipedOutputStream对象 如果此对象已经连接到其他某个管道输出流,则抛出
		// IOException
		in = new PipedInputStream(sender.getPipedOutputStream());
	}

	@Override
	public void run() {
		try {
			int data;
			while ((data = in.read()) != -1)
				System.out.println(data);
			in.close();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public static void main(String args[]) throws Exception {
		Sender sender = new Sender();
		Receiver receiver = new Receiver(sender);
		sender.start();
		receiver.start();
	}
}


   线程Sender向管道输出流中写字节,线程Receiver 从管道输入流中读取字节。线程Sender输出的字节序列与线程 Receiver读入的字节序列相同。
分享到:
评论
2 楼 binge520 2013-07-24  
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        
1 楼 binge520 2013-07-24  
                                                                                                                                                                                                                                                                                                                                                                                                              

相关推荐

    一个输入流写到多个输出流

    在IT行业中,输入流(InputStream)和输出流(OutputStream)是Java.IO库中的核心概念,它们用于处理数据的读取和写入。标题提到的"一个输入流写到多个输出流"是一个常见的需求,特别是在并发编程和数据传输场景中。...

    JAVA_管道流

    PipedInputStream是一个输入流,它接收从PipedOutputStream写入的数据。而PipedOutputStream则是一个输出流,它将数据写入到与之连接的PipedInputStream中。两者通过建立连接形成管道,实现数据的双向流动。 创建...

    Java管道流.pdf

    1. 管道输出流(PipedOutputStream)与管道输入流(PipedInputStream) PipedOutputStream是OutputStream的直接子类,负责向管道中写入字节数据;而PipedInputStream是InputStream的直接子类,负责从管道中读取字节...

    将输出流OutputStream转化为输入流InputStream的方法

    在实际开发中,`PipedOutputStream`和`PipedInputStream`也可以用来实现输出流和输入流之间的连接,它们之间可以通过管道进行数据传输,但这种方式相对复杂,适用于更高级的同步和异步操作。 总结,将`OutputStream...

    JAVA输入流和输出流

    PipedInputStream和PipedOutputStream允许在不同的线程间传递数据,实现数据的管道传输。DataInputStream和DataOutputStream提供了一种方式来读写基本的Java数据类型,如int、float和double。 在提供的`FileOut....

    java管道流

    这种设计导致了一个问题:当输出流写入20个字节后,输入流只能读取10个字节,这会导致剩余的10个字节仍然留在缓冲区中,直到下一次读取时才能被读取。 ### 总结 通过上述分析可以看出,管道流是一种非常有用的工具...

    JAVA管道流.pdf

    PipedInputStream是一个输入流,它可以从与之相连的PipedOutputStream获取数据。PipedOutputStream则是输出流,用于写入数据到PipedInputStream。通过将System.out和System.err的打印流替换为PipedOutputStream,...

    重难点之Java管道流.pdf

    通过调用`PipedOutputStream`的`connect`方法,将输出流与输入流关联起来,从而建立管道连接。 3. 数据传输 `Send`线程通过`write`方法将字符串转换为字节数组并写入到`PipedOutputStream`,而`Receive`线程通过`...

    Java IO流文档

    - `PipedInputStream`:管道输入流,用于线程间的通信。 - `SequenceInputStream`:将多个输入流组合成一个输入流。 - **OutputStream**:代表数据目的的输出流。 - `ByteArrayOutputStream`:将数据输出到内存中...

    IO输入输出流源码例子

    - 输入流(Input Stream):用于从数据源读取数据,数据源可以是文件、网络连接或其他数据产生器。 - 输出流(Output Stream):用于向目的地写入数据,目的地可能是文件、显示器或其他数据接收器。 2. **流的...

    IO体系.java

    |--PipedOutputStream/:可以将管道输出流连接到管道输入流来创建通信管道。 | 用方法connect(PipedInputStream snk) 将此管道输出流连接到接收者。 同样使用多线程技术,避免死锁。 |--ByteArrayOutputStream/:...

    java的IO流介绍

    管道流允许线程间的直接通信,数据可以从一个线程的输出流直接传递到另一个线程的输入流。 9. 标准流(System.in, System.out, System.err): 标准输入流、标准输出流和标准错误输出流是Java应用程序与用户的默认...

    JAVA输入与输出 java流基本流程图

    在Java中,输入和输出操作通过流(Stream)进行,这些流分为字节流和字符流两大类,每类又包含输入流和输出流。 **字节流** 字节流处理的是8位的字节数据,它主要用于处理二进制数据,如图像、音频文件等。Java的...

    JDK_API_1_6

    PipedInputStream 管道输入流应该连接到管道输出流;管道输入流提供要写入管道输出流的所有数据字节。 PipedOutputStream 可以将管道输出流连接到管道输入流来创建通信管道。 PipedReader 传送的字符输入流。 ...

    Java io输入输出流及字符集

    流可以分为四类:字节流(Byte Stream)和字符流(Character Stream),以及输入流(Input Stream)和输出流(Output Stream)。字节流处理单个字节的数据,而字符流处理Unicode字符,如UTF-8编码。 1. 字节流 - ...

    Java的输入输出流.pdf

    管道流(PipedInputStream/PipedOutputStream/PipedReader/PipedWriter)则允许在不同的线程之间建立数据通道,实现数据的同步传输。 过滤流(FilterInputStream/FilterOutputStream/FilterReader/FilterWriter)是...

    java 进程间利用管道通信实例

    两者需要通过构造函数相互关联,以便数据可以从输出流流向输入流。这个过程通常涉及在两个线程之间进行,一个线程负责写入,另一个线程负责读取,确保数据的同步传输。 在“Pipe”这个压缩包中,可能包含了一个...

    Java输入输出流

    输入流主要用于读取数据,它是所有字节输入流的基类,位于`java.io`包下。几个重要的子类包括: 1. `FileInputStream`: 用于从文件中读取数据。 2. `BufferedInputStream`: 提供缓冲功能,提高读取效率。 3. `...

    Java 输入与输出流操作

    - **InputStream** 和 **OutputStream** 是所有字节输入流和输出流的基类,用于处理二进制数据,如图像、音频等。 - **FileInputStream** 和 **FileOutputStream** 用于文件的读写操作。 - **BufferedInputStream...

    javaIO(java中的流的介绍)

    - InputStream:所有输入流的基类,用于读取原始字节。它定义了基本的读取方法,如read()用于获取单个字节。 - ByteArrayInputStream:从字节数组创建的输入流,可以从内存中读取数据。 - ...

Global site tag (gtag.js) - Google Analytics