import java.io.IOException;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class PipedStream {

/**
  * @param args
  * @throws IOException
  */
 public static void main(String[] args) throws IOException {

PipedInputStream input = new PipedInputStream();
  PipedOutputStream output = new PipedOutputStream();
  
  input.connect(output);
  
  new Thread(new Input(input)).start();
  new Thread(new Output(output)).start();
  
 }

}

class Input implements Runnable{
 
 private PipedInputStream in;
 Input(PipedInputStream in){
  this.in = in;
 }
 public void run(){
  
  try {
   byte[] buf = new byte[1024];
   int len = in.read(buf);
   
   String s = new String(buf,0,len);
   
   System.out.println("s="+s);
   in.close();
  } catch (Exception e) {
   // TODO: handle exception
  }
  
 }
}

class Output implements Runnable{
 private PipedOutputStream out;
 Output(PipedOutputStream out){
  this.out = out;
 }
 public void run(){
  
  try {
   Thread.sleep(5000);
   out.write("hi,管道来了!".getBytes());
  } catch (Exception e) {
   // TODO: handle exception
  }
 }
}

java 管道流代码示例的更多相关文章

  1. Java管道流PipedStream

    管道读取流和管道写入流可以像管道一样对接上,管道读取流就可以读取管道写入流写入的数据.需要注意的是需要加入多线程技术,因为单线程,先执行read,会发生死锁,因为read方法是阻塞式的,没有数据的re ...

  2. Java管道流

    管道流的主要作用可以用于两个线程之间的通信,有管道输出流 PipeOutputStream和管道输入流 PipeInputStream.然后通过connect将两个管道连接起来. import jav ...

  3. Java管道流学习

    管道流 作用:用于线程之间的数据通信 管道流测试:一个线程写入,一个线程读取 import java.io.IOException; import java.io.PipedInputStream; ...

  4. Jquery简单瀑布流代码示例

    最近很多网站都采用瀑布流风格设计,感觉挺有个性的,比较合适做图片类型的网站,没事仿开心网做一个瀑布流示例. 需要用到Jquery,jquery.masonry.min.js <!DOCTYPE ...

  5. JNI Java调用C代码 示例

    Activity public class MainActivity extends ListActivity {     static {         System.loadLibrary(&q ...

  6. Java读写XML代码示例

    采用技术:dom4j 需要jar包:dom4j-1.6.1.jar 主类如下---- public class dom4jTestXML { /**  *   * 方法描述:读取XML文件示例 备 注 ...

  7. java 管道流PipedInputStream,PipedInputStream和随机访问文件 RandomAccessFile

    http://blog.csdn.net/zlp1992/article/details/50298195   给个链接自己去看吧.网上资料不是很多,而且自己也不想写了 RandomAccessFil ...

  8. java:管道流(线程间管道流)

    class Send implements Runnable{ PipedOutputStream pos = null; public Send() { this.pos = new PipedOu ...

  9. Java连接ActiveMQ代码示例(Producer和Consumer)

    import org.apache.activemq.ActiveMQConnection; import org.apache.activemq.ActiveMQConnectionFactory; ...

随机推荐

  1. CNZZ站点流量统计原理简析

    这是我的域名www.iyizhan.com.暂无内容,当中仅仅有一个页面index.html. 在index.html上放置了例如以下的 js 脚本: <script  src="ht ...

  2. jQuery制作水平多级下拉菜单

    本篇体验使用jQuery制作水平的.多级的.下拉菜单. 下拉菜单的html部分如下. <body> <nav class="main-nav"> <u ...

  3. TCP半连接和syn攻击(转)

    TCP半连接和syn攻击 转载 2014年04月06日 21:36:10 4243 摘自:http://blog.sina.com.cn/s/blog_54b5ea250100g2r8.html SY ...

  4. 在IOS 模拟器中 输入中文

    模拟器默认的配置种没有“小地球”,只能输入英文.加入中文方法如下: 找到模拟器的Settings--->General-->Keyboard-->International KeyB ...

  5. 如何优化JAVA代码

    通过使用一些辅助性工具来找到程序中的瓶颈,然后就可以对瓶颈部分的代码进行优化.一般有两种方案:即优化代码或更改设计方法.我们一般会选择后者,因为不去调用以下代码要比调用一些优化的代码更能提高程序的性能 ...

  6. Java:字符串类简单的正则表达式

    class Test { public static void main(String[] args) { String str = "xia..as....yuan.com"; ...

  7. kth-smallest-element-in-a-sorted-matrix

    //有很多讨论,比如 // https://discuss.leetcode.com/topic/52865/my-solution-using-binary-search-in-c // https ...

  8. 第三章 LinkedList源码解析

    一.对于LinkedList需要掌握的八点内容 LinkedList的创建:即构造器 往LinkedList中添加对象:即add(E)方法 获取LinkedList中的单个对象:即get(int in ...

  9. Maximum Depth of Binary Tree leetcode java

    题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...

  10. ListView 设置列对齐方式

    <ListView.Resources> <Style TargetType="ListViewItem"> <Setter Property=&qu ...