这篇文章主要为大家详细介绍了java多线程编程之线程间的通信,探讨使用管道进行通信,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
上一章节讲了wait/notify通信,这一节我们来探讨使用管道进行通信。
java中提供了IO流使我们很方便的对数据进行操作,pipeStream是一种特殊的流,用于不同线程间直接传送数据。一个线程将数据发送到输出管道,另一个线程从输入管道读取数据。通过管道实现通信不需要借助临时文件这类东西。
java中提供了四个类使得线程间可以通信:
①字节流:PipeInputStream,PipedOutputStream
②字符流:PipedReader,PipedWriter
下面我们看看字节流的实现方法:
package pipeInputOutput; //输出流 import java.io.IOException; import java.io.PipedOutputStream; public class WriteDate { public void writeMethod(PipedOutputStream out) { try { System.out.println("write:"); for(int i=0;i<300;i++) { String outDate=""+(i+1); out.write(outDate.getBytes()); System.out.print(outDate); } System.out.println(); out.close(); }catch(IOException e) { e.printStackTrace(); } } }
package pipeInputOutput; //输入流 import java.io.IOException; import java.io.PipedInputStream; public class ReadDate { public void ReadDate(PipedInputStream input) { try { System.out.println("read:"); byte[] byteArray=new byte[20]; int readLength=input.read(byteArray); while(readLength!=-1) { String newDate=new String(byteArray,0,readLength); System.out.print(newDate); readLength=input.read(byteArray); } System.out.println(); input.close(); }catch(IOException e){ e.printStackTrace(); } } }
package pipeInputOutput; import java.io.PipedOutputStream; //输出线程 public class ThreadWrite extends Thread { private WriteDate write; private PipedOutputStream out; public ThreadWrite(WriteDate write,PipedOutputStream out) { super(); this.write=write; this.out=out; } public void run() { write.writeMethod(out); } }
package pipeInputOutput; import java.io.PipedInputStream; //输入线程 public class ThreadRead extends Thread{ private ReadDate read; private PipedInputStream in; public ThreadRead(ReadDate read,PipedInputStream in) { super(); this.read=read; this.in=in; } public void run() { read.ReadDate(in); } }
package pipeInputOutput; import java.io.IOException; import java.io.PipedInputStream; import java.io.PipedOutputStream; //测试方法 public class Run { public static void main(String[] args) { try { WriteDate write=new WriteDate(); ReadDate read=new ReadDate(); PipedInputStream inputStream=new PipedInputStream(); PipedOutputStream outputStream=new PipedOutputStream(); //输出流与输入流进行连接。 outputStream.connect(inputStream); //inputStream.connect(outputStream); ThreadRead readThread=new ThreadRead(read,inputStream); readThread.start();//先启动输出线程 Thread.sleep(2000); ThreadWrite writeThread=new ThreadWrite(write,outputStream); writeThread.start();//后启动输入线程 } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }
控制台输出:
read:
write:
123456789101112131415161718192021...
123456789101112131415161718192021...
上面测试中,先启动输入线程,然后因为没有线程被写入所以线程被阻塞,知道有数据写入。
我们接着继续看看字符流的实现方法:
package pipeInputOutput1; import java.io.IOException; import java.io.PipedWriter; //字符输出流 public class WriteDate { public void writeMethod(PipedWriter out) { try { System.out.println("write:"); for(int i=0;i<300;i++) { String outDate=""+(i+1); out.write(outDate); System.out.print(outDate); } System.out.println(); out.close(); }catch(IOException e) { e.printStackTrace(); } } }
package pipeInputOutput1; import java.io.IOException; import java.io.PipedReader; //字符输入流 public class ReadDate { public void readMethod(PipedReader in) { try { System.out.println("read:"); char[] byteArray=new char[20]; int readLength=in.read(byteArray); while(readLength!=-1) { String newDate=new String(byteArray,0,readLength); System.out.print(newDate); readLength=in.read(byteArray); } System.out.println(); in.close(); } catch (IOException e) { e.printStackTrace(); } } }
package pipeInputOutput1; import java.io.PipedWriter; //输出流线程 public class WriteThread extends Thread { private WriteDate write; private PipedWriter out; public WriteThread(WriteDate write,PipedWriter out) { super(); this.write=write; this.out=out; } public void run() { write.writeMethod(out); } }
package pipeInputOutput1; import java.io.PipedReader; //输入流线程 public class ReadThread extends Thread{ private ReadDate read; private PipedReader in; public ReadThread(ReadDate read,PipedReader in) { super(); this.read=read; this.in=in; } public void run() { read.readMethod(in); } }
package pipeInputOutput1; import java.io.IOException; import java.io.PipedReader; import java.io.PipedWriter; //测试方法 public class run { public static void main(String[] args) { try { WriteDate write=new WriteDate(); ReadDate read=new ReadDate(); PipedWriter out=new PipedWriter(); PipedReader in=new PipedReader(); //连接输出流与输入流 out.connect(in); //in.connect(out); ReadThread threadread=new ReadThread(read,in); threadread.start(); Thread.sleep(2000); WriteThread threadwrite=new WriteThread(write,out); threadwrite.start(); } catch (IOException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }
字符流额字节流大同小异,上面的例子中字符流不需要创建字节数组而已。
以上是Java多线程编之管道通信的实例分析的详细内容。更多信息请关注PHP中文网其他相关文章!

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于结构化数据处理开源库SPL的相关问题,下面就一起来看一下java下理想的结构化数据处理类库,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于PriorityQueue优先级队列的相关知识,Java集合框架中提供了PriorityQueue和PriorityBlockingQueue两种类型的优先级队列,PriorityQueue是线程不安全的,PriorityBlockingQueue是线程安全的,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于java锁的相关问题,包括了独占锁、悲观锁、乐观锁、共享锁等等内容,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于多线程的相关问题,包括了线程安装、线程加锁与线程不安全的原因、线程安全的标准类等等内容,希望对大家有帮助。

本篇文章给大家带来了关于Java的相关知识,其中主要介绍了关于关键字中this和super的相关问题,以及他们的一些区别,下面一起来看一下,希望对大家有帮助。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于枚举的相关问题,包括了枚举的基本操作、集合类对枚举的支持等等内容,下面一起来看一下,希望对大家有帮助。

封装是一种信息隐藏技术,是指一种将抽象性函式接口的实现细节部分包装、隐藏起来的方法;封装可以被认为是一个保护屏障,防止指定类的代码和数据被外部类定义的代码随机访问。封装可以通过关键字private,protected和public实现。

本篇文章给大家带来了关于java的相关知识,其中主要介绍了关于设计模式的相关问题,主要将装饰器模式的相关内容,指在不改变现有对象结构的情况下,动态地给该对象增加一些职责的模式,希望对大家有帮助。


热AI工具

Undresser.AI Undress
人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover
用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool
免费脱衣服图片

Clothoff.io
AI脱衣机

AI Hentai Generator
免费生成ai无尽的。

热门文章

热工具

DVWA
Damn Vulnerable Web App (DVWA) 是一个PHP/MySQL的Web应用程序,非常容易受到攻击。它的主要目标是成为安全专业人员在合法环境中测试自己的技能和工具的辅助工具,帮助Web开发人员更好地理解保护Web应用程序的过程,并帮助教师/学生在课堂环境中教授/学习Web应用程序安全。DVWA的目标是通过简单直接的界面练习一些最常见的Web漏洞,难度各不相同。请注意,该软件中

Atom编辑器mac版下载
最流行的的开源编辑器

Dreamweaver Mac版
视觉化网页开发工具

PhpStorm Mac 版本
最新(2018.2.1 )专业的PHP集成开发工具

SecLists
SecLists是最终安全测试人员的伙伴。它是一个包含各种类型列表的集合,这些列表在安全评估过程中经常使用,都在一个地方。SecLists通过方便地提供安全测试人员可能需要的所有列表,帮助提高安全测试的效率和生产力。列表类型包括用户名、密码、URL、模糊测试有效载荷、敏感数据模式、Web shell等等。测试人员只需将此存储库拉到新的测试机上,他就可以访问到所需的每种类型的列表。