本帖最后由 无气打火机 于 2013-7-29 18:26 编辑
- class Read implements Runnable {
- private PipedInputStream in;
- Read(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);
- in.close();
- }
- catch (IOException e) {
- throw new RuntimeException("读取失败");
- }
- }
- }
- class Write implements Runnable {
- private PipedOutputStream out;
- Write(PipedOutputStream out) {
- this.out=out;
- }
- public void run() {
- try {
- out.write("piped lai la".getBytes());
- out.close();
- }
- catch (IOException e) {
- throw new RuntimeException("输出败");
- }
- }
- }
- class PipedStreamDemo {
- public static void main(String[] args)throws IOException {
- PipedInputStream in = new PipedInputStream();
- PipedOutputStream out = new PipedOutputStream();
- in.connect(out);
- Read r = new Read(in);
- Write w =new Write(out);
- new Thread(r).start();
- new Thread(w).start();
- }
- }
复制代码 这里使用了多线程,都说管道流是这边进那边出,现在使用多线程,如果输出那边抢到了执行权
但是还没有数据,为什么也能成功输出????????? |