📄 ch20_11.java
字号:
import java.io.*;
class Ch20_11
{
public static void main(String args[])
{
PipedOutputStream out=null;
PipedInputstream in=null;
try
{
out=new PipedOutputStream();
in=new PipedInputStream();
in.connect(out);
}
catch(IOException e) {}
线程1 one =new 线程(out, in);
线程2 two=new 线程2(in, out);
one.start(); two.start();
}
}
class 线程1 extends Thread
{
PipedOutputStream out;
PipedInputStream in;
byte b[]={1, 2, 3};
线程1(PipedOutputStream a, PipedInputStream b)//构造方法获取输入和输出管道
{
try
{
out=a;
in=b;
out.connect(in); // 二者连接
}
catch(IOException e)
{}
}
public void run()
{
try
{
out.write(b, 0, 3);
} //写数据到输出管道
catch(IOException e)
{}
}
}
class 线程2 extends Thread
{
PipedInputStream in; PipedOutputStream out;
byte a[]=new byte[3];
线程2(PipedInputStream a, PipedOutputStream b) //构造方法获取输入和输出管道
{
try
{
in=a;
out=b;
in.connect( out); //将二者连接
}
catch(IOException e)
{}
}
public void run()
{
try
{
in.read(a, 0, 3);
for(int i=0; i<=2 ;i++)
{
System.out.println(" "+a[i]);
} //从输入管道读取数据
int c=a[0]+a[1]+a[2];
System.out .println(" "+c);
}
Catch(IOException e) {}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -