阿里Java学习路线:阶段 1:Java语言基础-Java语言高级特性:第17章: IO操作深入:课时82:管道流

管道流主要的功能是实现两个线程之间的IO处理操作。
在这里插入图片描述
对于管理流也是分为两类:
字节管道流:PipedOutputStream、PipedInputStream;
——连接处理:public void connect(PipedInputStream snk) throws IOException;
字符管道流:PipedWriter、PipedReader;
——连接处理:public void connect(PipedReader snk) throws IOException;
在这里插入图片描述
在这里插入图片描述
范例:实现管道操作

package cn.mldn.demo;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
public class JavaAPIDemo {
    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();
    }
}
class SendThread implements Runnable {
	private PipedOutputStream output ; // 管道的输出流
	public SendThread() {
		this.output = new PipedOutputStream(); // 实例化管理输出流
	}
	@Override
	public void run() {
		for (int x = 0; x < 10; x++) {
			try { // 利用管道实现数据的发送处理
				this.output.write(
						("【第"+ (x + 1) + "次信息发送- " + Thread.currentThread().getName() + "】www.mldn.cn\n").getBytes());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		try {
			this.output.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public PipedOutputStream getOutput() {
		return output;
	}
}
class ReceiveThread implements Runnable {
	private PipedInputStream input ;
	public ReceiveThread() {
		this.input = new PipedInputStream() ;
	}
	@Override
	public void run() {
		byte data [] = new byte [1024];
		int len = 0;
		ByteArrayOutputStream bos = new ByteArrayOutputStream(); // 所有的数据保存到内存输出流
		try {
			while((len = this.input.read(data)) != -1) {
				bos.write(data,0,len); // 所有的数据保存到内存流
			}
			System.out.println(" {" + Thread.currentThread().getName() + "接收消息} \n " + new String(bos.toByteArray()));
			bos.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		try {
			this.input.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	public PipedInputStream getInput() {
		return input;
	}
}

管道就类似于医院打点滴效果,一个只是负责发送,一个负责接收,中间靠一个管道连接。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
`io.grpc:protoc-gen-grpc-java:1.0.0:exe:${os.detected.classifier}` 是一个 Maven 坐标,用于使用 gRPC 的 Protocol Buffers 编译器插件来生成 gRPC 相关的 Java 代码。 这个坐标指定了以下部分: - `io.grpc` 是 Maven 组织 ID,表示该插件是由 gRPC 提供的。 - `protoc-gen-grpc-java` 是插件的名称,用于生成 gRPC 相关的 Java 代码。 - `1.0.0` 是插件的版本号,表示要使用的插件版本。 - `exe:${os.detected.classifier}` 指定了插件的文件类型和操作系统相关的后缀。 `${os.detected.classifier}` 是一个 Maven 变量,用于根据操作系统自动选择相应的插件文件。它会根据当前操作系统选择适当的文件后缀,例如在 Windows 上是 `.exe`,在 Linux 上是 `.linux-x86_64`。 通过在 Maven 项目的 `pom.xml` 文件中添加该依赖项,您可以在构建过程中自动下载并使用该插件来生成 gRPC 的 Java 代码。例如: ```xml <plugins> <plugin> <groupId>org.xolstice.maven.plugins</groupId> <artifactId>protobuf-maven-plugin</artifactId> <version>0.6.1</version> <configuration> <protocArtifact>com.google.protobuf:protoc:3.17.3:exe:${os.detected.classifier}</protocArtifact> <pluginId>grpc-java</pluginId> <pluginArtifact>io.grpc:protoc-gen-grpc-java:1.0.0:exe:${os.detected.classifier}</pluginArtifact> </configuration> <executions> <execution> <goals> <goal>compile</goal> <goal>compile-custom</goal> </goals> </execution> </executions> </plugin> </plugins> ``` 这样配置后,您可以使用 `mvn compile` 命令来自动生成 gRPC 的 Java 代码。生成的代码将位于 `target/generated-sources/protobuf` 目录下。 请确保您的 Maven 项目中已经包含了正确的依赖项,并且配置文件中的版本号与您所需的版本一致。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值