Java中管道报错:Write end dead
来源:互联网 发布:mac landesk 卸载 编辑:程序博客网 时间:2024/06/04 18:53
今天看了下关于管道的通信,Java中的管道只能在同一进程的不同线程间通信。今天测试两个线程进行通信发现报错。下面是我测试的代码。
package com.wpl.testIO;import java.io.IOException;import java.io.PipedInputStream;import java.io.PipedOutputStream;public class IoOne {@SuppressWarnings("resource")public static void main(String[] args) throws IOException {final PipedOutputStream outputStream=new PipedOutputStream();final PipedInputStream inputStream=new PipedInputStream(outputStream);Thread t1=new Thread(new Runnable() {@Overridepublic void run() {try {outputStream.write("Hello world".getBytes());//outputStream.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}});Thread t2=new Thread(new Runnable() {@Overridepublic void run() {try {int count=inputStream.read();while(count!=-1&&outputStream!=null){System.out.print((char) count);count=inputStream.read();}//inputStream.close();} catch (IOException e) {e.printStackTrace();}}});t1.start();t2.start();}}
报错的图片如下。
值能够读出来,但是最后还是会报错,不知道为何,往上看了很多解决办法,也没有用,同时我的输入和输出并没有显示的关闭,而是使用jdk1.7中的try-resources代替显示地调用close方法的方式。后来发现问题就出在这里将代码简单改写下,就没有报错了。
package com.wpl.testIO;import java.io.IOException;import java.io.PipedInputStream;import java.io.PipedOutputStream;public class IoOne {public static void main(String[] args) throws IOException {final PipedOutputStream outputStream=new PipedOutputStream();final PipedInputStream inputStream=new PipedInputStream(outputStream);Thread t1=new Thread(new Runnable() {@Overridepublic void run() {try {outputStream.write("Hello world".getBytes());outputStream.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}});Thread t2=new Thread(new Runnable() {@Overridepublic void run() {try {int count=inputStream.read();while(count!=-1&&outputStream!=null){System.out.print((char) count);count=inputStream.read();}inputStream.close();} catch (IOException e) {e.printStackTrace();}}});t1.start();t2.start();}}
其实还是资源没有关闭的问题,下次应该注意。
阅读全文
1 0
- Java中管道报错:Write end dead
- java中dead code
- 使用write报错
- flume kafka sparkstreaming整合后spark executor dead 及集群报错java.io.IOException: Connection reset by peer
- java报错:Connection reset by peer: socket write error
- samba报错:smbd dead but pid file exists
- end $ delimiter ;报错问题
- write EIO nodejs报错
- Java中管道流
- Java中write()系列方法
- java报错:The reference to entity "characterEncoding" must end with the ';' delimiter.
- java报错:The reference to entity "characterEncoding" must end with the ';' delimiter.
- java报错:The reference to entity "characterEncoding" must end with the ';' delimiter.
- struts中 bean:write name="xxx" property="非String" 报错
- jxl问题,报错如下 Exception in thread "main" java.lang.NoClassDefFoundError: jxl/write/WritableCell
- testng报错"java.net.SocketException: Software caused connection abort: socket write error"
- testng报错"java.net.SocketException: Software caused connection abort: socket write error"
- 关于java.lang.NoClassDefFoundError: jxl/write/WritableCell报错的个人解决方法
- Android 5.1高通方案增加GPIO按键
- php判断 !==false
- SSL peer shut down incorrectly
- Head First JavaScript 存储数据
- 数据增强:DataAugmentation
- Java中管道报错:Write end dead
- java中static关键字理解
- oracle数据库存储迁移参考
- c#读取lua嵌套表格
- 解决ScrollView嵌套RecyclerView 滑动卡顿和嵌套多个RecyclerView 显示不全的问题
- Eclipse:An internal error occurred during: "Build Project". GC overhead limit exceeded
- Unix系统的发展简介
- python杂记
- eclipse下tomcat启动器执行两次