Java 中利用管道实现线程间的通讯

本文介绍Java中的管道流,一种用于线程间直接传送数据的特殊流。通过具体实例pipeapp,展示如何创建管道流,并在多个线程间传递数据,实现从文件读取数据,经过两次转换后再显示。

 在Java 语言中,提供了各种各样的输入输出流(stream),使我们能够很方便的对数据进行操作,其中,管道 (pipe)流是一种特殊的流,用于在不同线程(threads)间直接传送数据。一个线程发送数据到输出管道 ,另一个线程从输入管道 中读数据。通过使用管道 ,实现不同线程间的通讯。 无需求助于类似临时文件 之类的东西。本文在简要介绍管道 的基本概念后,将以一个具体的实例pipeapp加以详细说明。   1.管道 的创建与使用

  Java 提供了两个特殊的专门的类专门用于处理管道 ,它们就是pipedinputstream类和pipeoutputstream类。

  Pipedinputstream代表了数据在管道 中的输出端,也就是线程向管道 读数据的一端;pipeoutputstream代表了数据在管道 中的输入端,也就是线程向管道 数据的一端,这两个类一起使用可以提供数据的管道 流。

  为了创建一个管道 流,我们必须首先创建一个pipeoutstream对象,然后,创建pipeinputstream对象,实例如下:

pipeout= new pipedyoutstream();
pipein= new pipedputsteam(pipepout);

  一旦创建了一个管道 后,就可以象操作文件 一样对管道 进行数据的读 。   2.演示程序: pipeapp

  应用程序由三个程序组成:主线程(pipeapp.Java )及由主线程启动的两个二级线程(ythread.Java 和zthread.Java ),它们使用管道 来处理数据。程序从一个内容为一行一行"x"字母的"input.txt"文件 中读取数据,使用管道 传输数据,第一次是利用线程ythread将数据"x"转换为"y",最后利用线程zthread将"y"转换为"z",之后,程序在屏幕上显示修改后的数据。

  主线程 (pipeapp.Java

  在main()方法中,程序首先创建一个应用对象:pipeapp pipeapp=new pipeapp();

由于程序中流操作都需要使用IOException异常处理,所以设置了一个try块。在try中,为了从源文件 中读取数据,程序为"input.txt"文件 创建了一个输入流Xfileln,:

fileinputstream xfileln= new fileinputstream("input.txt");

  新的输入流传递给changetoy()方法,让线程ythread能读取该文件

  inputstream ylnpipe =pipeapp.changetoy(xfileln);

  changetoy()方法创建将输入数据"x"改变到"y"的线程ythread,并返回该线程的输入管道

  inputstream zlnpipe = pipeapp.changetoz(ylnpipe);

  changetoz()方法启动将数据从"y"改变到"z"的线程zehread,主程序将使用从changetoz()返回的输入管道 。得到以修改的数据。

  然后,程序将管道 输入流定位到datainputstream对象,使程序能够使用readline()方法读取数据:
  datainputstream inputstream = new datainputstream(zlnpiepe);

  创建了输入流以后,程序就可以以行一行的读取数据病显示在屏幕上。

  String str= inputstream.readline();
  While(str!=null)
  {
    system.out.println(str);
    str=inputstream.readline();
  }

  显示完成之后,程序关闭输入流:

  inputstream.close();
  changetoy()方法

  changetoy()方法首先通过传递一个参数inputstream给datainputstream对象来定位资源的输入流,使程序能使用readline()方法从流中读取数据:

  datainputstream xfileln =new datainutstream(inputstream);

  然后,changetoy()创建输出管道 和输入管道

  pipeoutstream pipeout = new pipeoutputstream();
  pipeinputstream pipeln = new pipedinputsteam(pipeout);

  为了能够使用println()方法输出修改的后的文本行到管道 ,程序将输出管道 定位到printstream对象:

  printstream printstream = new printstream(pipeout);

  现在,程序可以创建将数据从x改变到y的线程,该线程是ythread类的一个对象,他传递两个参数:输入文件 (xfileln)和输出管道 (调用printstream)

  ythread ythread =new thread(xfileln,printstream);

  之后,程序启动线程:

  changetoz()方法

  changetoz()方法与changetoy()方法很相似,他从changetoy()返回的输入流开始:

  datainputstream yfileln= new datainputstream(inputstream);

  程序创建一个新的管道

  pipedoutstream pipeout2 = new pipedoutputstream();
  pipedinputstream pipeln2 = new pipedinputsream(pipeout2);

  该线程通过这个新的管道 发出修改后的数据(输入流pipeln2)给主程序。

源程序如下:

import Java.io.*
class pipeapp
{
public static void main(string[] args)
{
pipeapp pipeapp=new pipeapp();
try
{
fileinputstream xfile =new fileinputstream("input.txt");
inputstream ylnpipe = pipeapp.changetoy(xfileln);
inputstream zlnpipe=pipeapp.changetoz(ylnpipe);
system.out.println();
system.out.println("here are the results");
system.out.pringln();
datainputstream inputstream = nes datainputstream(zlnpipe);
string str = inputstream.readline();
while (str!=null)
{
system.out.println(str);
str=inputstream.readline();
}
inputstream.close();
}
catch(exception e)
{
system.out.println(e.tostring());
}
}
public inputstream changetoy(inputstream inputstream)
{
try
{
datainputstream pipeout = new datainputsteam(inputstream);
pipedoutstream pipeout = new pipedoutputstream();
pipedlnsteam pipeln = new pipedlnputstream(pipeout);
printstream printstream = new printstream(pipeout);
ythread ythread = new ythread(xfileln,printstream);
ythread.start();
return pipeln;
}
catch(exeption e)
{
system.out.println(x.tostring());
}
return null;
}
public inputstream changetoz(inputstream inputsteam)
{
try
{
datainputstream yfileln = new datainputstream(inputstream);
pipeoutputstream pipeln2 = new pipedinputstream(pipeout2);
printrstream printstream2 = new printsteam(pipeout2);
zthread zthread = new zthread(yfileln,printstream2);
zthread.start();
return pipeln2;
}
catch(exception e)
{
system.out.println(e.tostring());
}
return null;
}
}

  Ythread类和Zthread类

由于ythread类与zthread类基本一样,在此仅以ythread为例加以说明。

  Ythread的构造器接收两个参数:输入的文件 和第一个管道 的输出端,构造器存储这两个参数作为类的数据成员:

  Ythread(datainputstream xfileln,pringstream printstream)
  {
    this.xfileln = xfileln;
    this.printstream = printstream;
  }

  线程通过run()方法来处理数据。首先读取一行数据,确保xstring不为空的情况下循环执行:
string xstring = xfileln.readline();

  每读一行数据,完成一次转换

  string ystring = xstring.replace('x','y');

  然后将修改后的数据输出到管道 的输出端:

  prinstream.prinrln(ystring);

  为了确保所有缓冲区的数据完全进入管道 的输出端:

  pringstram.flush();

  循环完成后,线程关闭管道 输出流:

  pringstram.close();

ythread类的源程序如下:

import Java.io.*;
class ythread exteads thread
{
datainputstream xfileln;
pringstream printstream;
ythread(datainputstream xfileln,pringstream.printstream)
{
this.xfileln = xfileln;
this.printstream = printstream;
}
public void run()
{
try
{
string xstring = xfileln.readline();
while(xstring!=null)
{
string ystring= xstring.replace('x','y');
printstream.pringln(ystring);
printstream.flush();
xstring= xfileln.readline();
}
printstream.close();
}
catch{ioexception e}
{
system.out.println(e.tostring());
}
}
}
 
(1)普通用户端(全平台) 音乐播放核心体验: 个性化首页:基于 “听歌历史 + 收藏偏好” 展示 “推荐歌单(每日 30 首)、新歌速递、相似曲风推荐”,支持按 “场景(通勤 / 学习 / 运动)” 切换推荐维度。 播放页功能:支持 “无损音质切换、倍速播放(0.5x-2.0x)、定时关闭、歌词逐句滚动”,提供 “沉浸式全屏模式”(隐藏冗余控件,突出歌词与专辑封面)。 多端同步:自动同步 “播放进度、收藏列表、歌单” 至所有登录设备(如手机暂停后,电脑端打开可继续播放)。 音乐发现与管理: 智能搜索:支持 “歌曲名 / 歌手 / 歌词片段” 搜索,提供 “模糊匹配(如输入‘晴天’联想‘周杰伦 - 晴天’)、热门搜索词推荐”,结果按 “热度 / 匹配度” 排序。 歌单管理:创建 “公开 / 私有 / 加密” 歌单,支持 “批量添加歌曲、拖拽排序、一键分享到社交平台”,系统自动生成 “歌单封面(基于歌曲风格配色)”。 音乐分类浏览:按 “曲风(流行 / 摇滚 / 古典)、语言(国语 / 英语 / 日语)、年代(80 后经典 / 2023 新歌)” 分层浏览,每个分类页展示 “TOP50 榜单”。 社交互动功能: 动态广场:查看 “关注的用户 / 音乐人发布的动态(如‘分享新歌感受’)、好友正在听的歌曲”,支持 “点赞 / 评论 / 转发”,可直接点击动态中的歌曲播放。 听歌排行:个人页展示 “本周听歌 TOP10、累计听歌时长”,平台定期生成 “全球 / 好友榜”(如 “好友中你本周听歌时长排名第 3”)。 音乐圈:加入 “特定曲风圈子(如‘古典音乐爱好者’)”,参与 “话题讨论(如‘你心中最经典的钢琴曲’)、线上歌单共创”。 (2)音乐人端(创作者中心) 作品管理: 音乐上传:支持 “无损音频(FLAC/WAV)+ 歌词文件(LRC)+ 专辑封面” 上传,填写 “歌曲信息
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值