class1.java

来自「矩阵乘法的计算程序」· Java 代码 · 共 65 行

JAVA
65
字号
import java.io.*;
public class Class1
{
	/**
	 * The main entry point for the application. 
	 *
	 * @param args Array of parameters passed to the application
	 * via the command line.
	 */
	public static void main (String[] args)throws IOException 
	{
		PipedInputStream in=new PipedInputStream();
		PipedOutputStream out=new PipedOutputStream();
		out.connect(in);
		System.out.println("管道已经成功建立!");
		Thread t1=new sender(out);
		Thread t2=new receiver(in);
		t1.start();
		t2.start();
		// TODO: Add initialization code here
	}
}

class sender extends Thread 
{
	private DataOutputStream sendstream;
	public sender(PipedOutputStream out){
		sendstream=new DataOutputStream(out);
	}
	public void run() {
	String s="你好,接收者!这是管道流通信测试";
	try{
    	sendstream.writeUTF(s);
        sendstream.close();
	   }
	catch(IOException e)
	   {
		System.out.println("error:"+e);
		System.exit(-1);
	    }
		
    }
}

class receiver extends Thread 
{
	private DataInputStream receivestream;
	public receiver(PipedInputStream in){
		receivestream=new DataInputStream(in);
	}
	public void run(){
	String s;
	try{	
		s=receivestream.readUTF();
        System.out.println("下列信息来自发送者:\n"+s);
		receivestream.close();
		while (System.in.read()!='1'){}
	   }
	catch(IOException e)
	   {
		System.out.println("error:"+e);
		System.exit(-1);
	    }	
    }
}

⌨️ 快捷键说明

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