zoukankan      html  css  js  c++  java
  • PipedInputStream

    http://www.cnblogs.com/meng72ndsc/archive/2010/12/23/1915358.html

    ----------------------------------------------
    import
    java.io.IOException;
    import java.io.PipedOutputStream;

    public class Producer extends Thread {
    private PipedOutputStream pos;

    public Producer(PipedOutputStream pos) {
    this.pos = pos;
    }

    @Override
    public void run() {
    super.run();
    try {
    pos.write(
    "Hello".getBytes());
    }
    catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    ----------------------------------------------------
    import java.io.IOException;
    import java.io.PipedInputStream;

    public class Consumer extends Thread {
    private PipedInputStream pis;

    public Consumer(PipedInputStream pis) {
    this.pis = pis;
    }

    @Override
    public void run() {
    super.run();
    byte[] b = new byte[100]; // 将数据保存在byte数组中
    try {
    int len = pis.read(b); // 从数组中得到实际大小。
    System.out.println(new String(b, 0, len));
    pis.close();
    }
    catch (IOException e) {
    e.printStackTrace();
    }
    }
    }
    --------------------------------------------------
    import java.io.IOException;
    import java.io.PipedInputStream;
    import java.io.PipedOutputStream;

    public class PipedStreamTest {
    public static void main(String[] args) {
    PipedOutputStream pos
    = new PipedOutputStream();
    PipedInputStream pis
    = new PipedInputStream();
    try {
    pos.connect(pis);
    // 连接管道

    new Producer(pos).start();// 启动线程

    new Consumer(pis).start();// 启动线程

    }
    catch (IOException e) {
    e.printStackTrace();
    }
    }
    }

  • 相关阅读:
    December 23rd 2016 Week 52nd Friday
    December 22nd 2016 Week 52nd Thursday
    December 21st 2016 Week 52nd Wednesday
    December 20th 2016 Week 52nd Tuesday
    December 19th 2016 Week 52nd Sunday
    December 18th 2016 Week 52nd Sunday
    uva294(唯一分解定理)
    uva11624Fire!(bfs)
    fzu2150Fire Game(双起点bfs)
    poj3276Face The Right Way
  • 原文地址:https://www.cnblogs.com/bayonetxxx/p/1929054.html
Copyright © 2011-2022 走看看