pipedstreamtest.java

来自「孙鑫JAVA从入门到精通配套练习程序。所有程序都实际运行过」· Java 代码 · 共 57 行

JAVA
57
字号
import java.io.*;

class PipedStreamTest
{
	public static void main(String[] args) throws IOException
	{
		PipedInputStream pis = new PipedInputStream();
		PipedOutputStream pos = new PipedOutputStream();
		pos.connect(pis);
		new Producer(pos).start();
		new Consumer(pis).start();
	}
}

class Producer extends Thread
{
	private PipedOutputStream pos = new PipedOutputStream();
	Producer(PipedOutputStream pos)
	{
		this.pos = pos;
	}
	public void run()
	{
		try
		{
			pos.write("My name is oyqd".getBytes());
		}
		catch(IOException ex)
		{
			ex.printStackTrace();
		}
	}
}

class Consumer extends Thread
{
	private PipedInputStream pis = new PipedInputStream();
	Consumer(PipedInputStream pis)
	{
		this.pis = pis;
	}
	public void run()
	{
		try
		{
			byte[] data = new byte[100];
			int len = pis.read(data);
			System.out.println(new String(data, 0, len));
		}
		catch(IOException ex)
		{
			ex.printStackTrace();
		}
	}
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?