本帖最后由 Alan 于 2013-5-9 20:56 编辑
- package cn.itcast.io;
- import java.io.IOException;
- import java.io.PipedInputStream;
- import java.io.PipedOutputStream;
- import java.util.concurrent.ExecutorService;
- import java.util.concurrent.Executors;
- public class PipedDemo {
-
- public static void main(String[] args) throws Exception {
-
- final PipedOutputStream pipeout = new PipedOutputStream();
- final PipedInputStream pipein = new PipedInputStream(pipeout);
-
-
- ExecutorService pool = Executors.newCachedThreadPool();
-
- pool.execute(new Runnable(){
- @Override
- public void run() {
- try {
- pipeout.write("hello motou".getBytes());
- pipeout.close();
- } catch (IOException e) {
- throw new RuntimeException("shuchushibai");
- }
- System.out.println(Thread.currentThread());
- }
- });
- pool.execute(new Runnable(){
- @Override
- public void run() {
-
- try {
-
- byte[] bit = new byte[1024];
- int len = pipein.read(bit);
- System.out.println(new String(bit,0,len));
- pipein.close();
- } catch (IOException e) {
- throw new RuntimeException("shurushibai");
- }
- System.out.println(Thread.currentThread());
- }
- });
-
- }
- }
复制代码 |