ch20_11.java

来自「java练习程序」· Java 代码 · 共 77 行

JAVA
77
字号
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 + =
减小字号Ctrl + -
显示快捷键?