java 线程类 通信_Java 中利用管道实现线程间的通讯

在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)给主程序。

源程序如下:

//

//pipeapp.Java-pipeapp的主应用程序

//

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类的源程序如下:

//

//ythread.Java

//

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());

}

}

}

pipeapp应用程序使用microsoft visual j++1.1编译

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/442271.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

java整型缓存_JAVA整型包装类的缓存策略

Java Integer的缓存策略public classJavaIntegerCache {public static voidmain(String... strings) {Integer integer1 3;Integer integer2 3;if (integer1 integer2)System.out.println("integer1 integer2");elseSystem.out.println("integer1 ! integer2&q…

java年利润编程题_[编程入门]利润计算-题解(Java代码)

![](/image_editor_upload/20200216102044_14158.png)这也是简单的分类问题,用if进行分类后就可以用switch进行分类计算利润代码如下:package javaapplication;import java.util.Scanner;public class JavaApplication{public static void main(String[]…

java面向方面编程_面向方面编程的介绍----基本概念

面向对象的编程中常用的概念是:继承、封装、多态。在面向方面的编程中常使用的概念是:advices/interceptors, introductions, metadata, and pointcuts。AOP 面向方面编程的介绍----基本概念(3)面向方面的编程思路很简单。从面向过程、函数的编程到面向对…

链队列的基本运算java_链式队列基本操作的实现问题

问题描述:用链式存储方式实现队列的基本操作涉及变量:front:Node型自定义变量,指向队首元素rear:Node型自定义变量,指向队尾元素涉及教材:《数据结构——Java语言描述(第2版)》 清华大学出版社大…

mysql数据库优化看的书_MySQL 数据库优化,看这篇就够了

点击上方"IT牧场",选择"设为星标"技术干货每日送达!来源:segmentfault.com/a/1190000018631870前言数据库优化一方面是找出系统的瓶颈,提高MySQL数据库的整体性能,而另一方面需要合理的结构设计和参数调整,以提高用户的相…

python 升级所有库_自动更新Python所有第三方库

一般python用得比较久以后,就会安装很多第三方的库。比如这是我的pip list情况:pip list而且一屏还显示不完。通过如下命令可以看到需要更新的第三方库:pip list -o需要更新的库而pip提供的更新命令只能一个个的更新...pip install -U 库名 #…

java kryo register_java相关:Kryo框架使用方法代码示例

java相关:Kryo框架使用方法代码示例发布于 2021-1-21|复制链接摘记: Kryo框架的source已移至https://github.com/EsotericSoftware/kryo ,进入此页面,然后点击右边的Download Zip按钮,就能下载到最新版本的Kr ..Kryo框架的source已…

java 反射 类变量_java反射机制取出model类的所有变量,以及value

工作上遇到个问题,顺便解决了,希望对大家有帮助package com.zuidaima.util;public static void main(String[] args) throws ClassNotFoundException, IllegalArgumentException, IllegalAccessException, InstantiationException {Class> obj Class.forName("com.roi…

php 去掉不可见字符串,php去掉指定字符串的办法

php去掉指定字符串的办法:首先创建一个PHP示例文件;然后定义字符串;最后通过“str_replace(array("_","",""),"",$str);”方法去掉指定字符串即可。推荐:《PHP视频教程》用正则可以解决问…

php开发地图导航,php 实现百度地图(很详细出来的地图)

百度地图搜索--leyangjunvar map new BMap.Map("container");map.centerAndZoom(new BMap.Point(116.404, 39.915), 11);var local new BMap.LocalSearch(map, {renderOptions: {map: map, panel: "results"}});var opts {anchor: BMAP_ANCHOR_TOP_RIGH…

php查询表导出excel文件路径,Thinkphp5如何将导出的excel表格存储到服务器中项目目录的指定目录中?...

/**excel表格导出param string $fileName 文件名称param array $headArr 表头名称param array $data 要导出的数据author static7 */function excelExport($fileName , $headArr [], $data []) {$fileName . "_" . date("Y_m_d", Request::instance()-&…

php 大型系统开法流程图,有一个php项目源码,如何搞清楚执行过程?画出其流程图...

比如一个c项目,可以让其运行起来,然后利用gdb调试,一步步执行,搞清楚其执行过程,那么一个php项目,如何才能搞清楚执行过程呢,或者需要什么专业的工具如果你想调试的话,可以使用zend …

php如何判断文件的内容,php 判断文件内容是否存在的方法

php 判断文件是否存在的示例代码本篇内容为大家讲解的是php 判断文件是否存在,感兴趣的同学参考学习下,本文内容如下:sha1_file 计算文本文件SHA-1散列# 计算文本文件 $file 的 SHA-1 散列:sha1_file($file);# 语法sha1_file(file,raw)# 参数…

php文件上传并保存路径到数据库,thinkphp表单上传文件并将文件路径保存到数据库中?...

上传单个文件,此文以上传图片为例,上传效果如图所示创建数据库upload_img,用于保存上传路径CREATE TABLE seminar_upload_img ( id int(11) NOT NULL AUTO_INCREMENT, img_name varchar(255) DEFAULT NULL COMMENT 图片名称, img_url varchar(255) DE…

PHP数组的访问方法有几种,数组常用方法有哪些

数组中常用的方法有:1、给数组末尾添加新内容的push方法;2、删除数组最后一项的pop方法;3、删除数组第一项的shift方法;4、向数组首位添加新内容unshift方法等等。数组常用的一些方法:1、push()向数组的末尾添加新内容…

roaringbitmap java,roaringbitmap 源码解析 bitmap add过程

最近在做标签平台的分析引擎。底层涉及到位图的处理,所以涉及到roaringbitmap。

java opcode 反汇编,如何将VM的opcode嵌入汇编源码中

这次来一个关于VM的混淆办法,可能只是个小trick,仅仅来自胡思乱想也许你会觉得很奇怪,一个VM能有啥新鲜的,对,单纯来说VM保护源代码已经非常的成熟了,所以在这里只做最基本的介绍,而且这次的重点…

php ip 合法,什么是合法ip地址

合法的IP地址中,每个三位数都是在0~254之间的,不可能是大于254就连255都不行。这才是合法的IP地址,还有 IP地址有A\B\C类IP。iPv4的ip地址都是(1~255).(0~255).(0~255).(0~255)的格式。A类的IP地址范围为0.0.0.0-127.255.255.255B类的IP地址范…

java seteditable,Java TextField.setEditable方法代碼示例

import java.awt.TextField; //導入方法依賴的package包/類public void setupDBFields(String dbname) throws Exception{viewPane.setLayout(null);Dimension dimView sp.getSize();int height 0, width 50;viewPane.removeAll();db new DBF(dbname);setTitle(dbname);gb …

php session和cookie区别,php中session和cookie的区别是什么?

一、Session(1)Session的由来以及介绍Session:在计算机中,尤其是在网络应用中,称为“会话控制”,生存时间为用户在浏览某个网站时,从进入网站到关闭这个网站所经过的这段时间,也就是用户浏览这个网站所花费的时间。由于Http是一种…