Java refreshes the past and knows the new-pipeline flow

Pipe flow

Insert picture description here
Java itself is a multi-pure programming language, and the biggest difference between multi-threading and multi-process is that: a process can produce multiple threads, then these threads belong to the same process as a component unit, so these threads can be directly shared All resources in a given process, including data, but in Java it emphasizes that different threads may also have their own content, so different threads should also use pipelines for processing.

Java code:

package com.yootk.demo;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
class SendThread implements Runnable {
    
    
    private PipedOutputStream output = new PipedOutputStream() ;    // 管道输出流
    @Override
    public void run() {
    
    
        try {
    
       // 通过管道实现数据的发送
            this.output.write("李兴华编程训练营:yootk.ke.qq.com".getBytes());
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
    public PipedOutputStream getOutput() {
    
      // 通过子类操作
        return this.output ;
    }
}
class ReceiveThread implements Runnable {
    
    
    private PipedInputStream input = new PipedInputStream() ; // 管道输入流
    @Override
    public void run() {
    
    
        try {
    
    
            byte data [] = new byte [1024] ;
            int len = this.input.read(data) ; // 接收管道的数据
            System.out.println("【接收到消息】" + new String(data, 0, len));
        } catch (IOException e) {
    
    
            e.printStackTrace();
        }
    }
    public PipedInputStream getInput() {
    
    
        return this.input ;
    }
}
public class YootkDemo {
    
        // 李兴华编程训练营:yootk.ke.qq.com
    public static void main(String[] args) throws Exception {
    
    
        SendThread send = new SendThread() ; // 发送线程
        ReceiveThread receive = new ReceiveThread() ; // 接收线程
        send.getOutput().connect(receive.getInput()); // 管道连接
        new Thread(send).start();
        new Thread(receive).start();
    }
}

Guess you like

Origin blog.csdn.net/lxyoucan/article/details/113768465