文
章
目
录
章
目
录
Java多线程场景,线程与线程之间需要通信协作,之前我们讲了两篇文章Java多线程:线程间通信(等待/通知机制)、Java多线程:线程间通信(生产者消费者模式),而我们可以通过管道实现线程间通信。
案例场景
按照用户搜索条件查询数据,并将数据下载到用户本地,那系统通常的做法就是按照用户检索条件查询出结果集,将结果集生成excel表,在将excel表上传到ftp或者oss云盘之类的地方,最后将下载连接发送给用户,最后用户点击链接进行下载,那么使用管道就减少了数据生成到本地,在上传到云盘的工作,直接生成到用户下载,提高用户体验和效率。
管道使用方式
Java.io包的PipeStream管道流用于在线程之间传递数据,一个线程通过管道输出数据,另一个线程从管道中输入数据。相关类包括:
- PipedInputStream
- PipedOutputStream
- PipedReader
- PipedWriter
前两种是基础字节流,后两种是基础字符流。
代码案例
案例1:PipedInputStream和PipedOutputStream
使用PipedInputStream和PipedOutputStream在线程间传递字节流:
public class Test {
public static void main(String[] args) throws IOException {
//定义管道字节流
PipedInputStream in = new PipedInputStream();
PipedOutputStream out = new PipedOutputStream();
//建立管道之间的关系
in.connect(out);
//创建两个线程,分别往管道里写数据,和读数据
new Thread(new Runnable() {
@Override
public void run() {
try {
writeData(out);
} catch (IOException e) {
e.printStackTrace();
}
}
}, "Thread1").start();
new Thread(new Runnable() {
@Override
public void run() {
try {
readData(in);
} catch (IOException e) {
e.printStackTrace();
}
}
}, "Thread2").start();
}
//向管道流中写入数据
public static void writeData(PipedOutputStream out) throws IOException {
//分别把0~100的数据写入管道
try {
for(int i = 0; i <= 100; i++) {
//把字节数组写入到输出管道流中
out.write(("" + i).getBytes(StandardCharsets.UTF_8));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
out.close();
}
}
//从管道流中读取数据
public static void readData(PipedInputStream in) throws IOException {
int count = 0;
byte[] bytes = new byte[1024];
int len = 0;
try {
while((len = in.read(bytes)) != -1) {
System.out.println(new String(bytes, 0, len));
}
} catch (IOException e) {
e.printStackTrace();
} finally {
in.close();
}
}
}
案例2:PipedWriter和PipedReader
PipedWriter、PipedReader实现多线程从控制台读取用户输入输出:
public class PipedTeset {
public static void main(String[] args) throws IOException {
PipedReader in = new PipedReader();
PipedWriter out = new PipedWriter();
out.connect(in);
Thread printThread = new Thread(new PrintThread(in), "printThread");
printThread.start();
int receive = 0;
try {
while ((receive = System.in.read()) != -1) {
// 主线程进行读取控制台输入
out.write(receive);
}
} catch (RuntimeException e) {
}
}
static class PrintThread implements Runnable{
private PipedReader in;
public PrintThread(PipedReader in) {
this.in = in;
}
@Override
public void run() {
int receive;
try {
while ((receive = in.read()) != -1){
// printThread 收到字符然后输入,这里可以使用sokcet编程将字符传输到对应计算机中
System.out.print((char) receive);
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}
总结
以上就是Java多线程通过管道实现线程间通信的全部内容。