⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pipedemo.java

📁 Java网络编程与分布式计算, 主要分析java网络各方面的编程, 提供许多实用安全
💻 JAVA
字号:
import java.io.*;

// Chapter 7, Listing 8
public class PipeDemo extends Thread
{
	PipedOutputStream output;

	// Create an instance of the PipeDemo class
	public PipeDemo(PipedOutputStream out)
	{
		// Copy to local member variable
		output = out;
	}

	public static void main (String args[])
	{
		try
		{
			// Create a pipe for writing
			PipedOutputStream pout = new PipedOutputStream();

			// Create a pipe for reading, and connect it to output pipe
			PipedInputStream pin = new PipedInputStream(pout);

			// Create a new pipe demo thread, to write to our pipe
			PipeDemo pipedemo = new PipeDemo(pout);

			// Start the thread
			pipedemo.start();

			// Read thread data,
			int input = pin.read();

			// Terminate when end of stream reached
			while (input != -1)
			{
				// Print message
				System.out.print ( (char) input);

				// Read next byte
				input = pin.read();
			}
		}
		catch (Exception e)
		{
			System.err.println ("Pipe error " + e);
		}
	}

	public void run()
	{
		try
		{
			// Create a printstream for convenient writing
			PrintStream p = new PrintStream( output );

			// Print message
			p.println ("Hello from another thread, via pipes!");

			// Close the stream
			p.close();
		}
		catch (Exception e)
		{
			// no code req'd
		}
	}
}

⌨️ 快捷键说明

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