pipedemo.java
来自「Java网络编程与分布式计算, 主要分析java网络各方面的编程, 提供许多实用」· Java 代码 · 共 70 行
JAVA
70 行
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 + =
减小字号Ctrl + -
显示快捷键?