黑马程序员技术交流社区

标题: 对视频教程中管道流代码的一点小小的优化 [打印本页]

作者: 王金科    时间: 2012-8-20 18:26
标题: 对视频教程中管道流代码的一点小小的优化
教程中原代码:
  1. package cn.study.file;

  2. import java.io.*;

  3. public class  PipedStreamDemo1
  4. {
  5.         public static void main(String[] args) throws IOException
  6.         {
  7.                 PipedInputStream in = new PipedInputStream();
  8.                 PipedOutputStream out = new PipedOutputStream();
  9.                 in.connect(out);
  10.                 Read r = new Read(in);
  11.                 Write w = new Write(out);
  12.                
  13.                 new Thread(w).start();
  14.                 new Thread(r).start();
  15.         }
  16. }

  17. class Read implements Runnable
  18. {
  19.         private PipedInputStream in;
  20.         Read(PipedInputStream in)
  21.         {
  22.                 this.in = in;
  23.         }
  24.         public void run()
  25.         {
  26.                 try
  27.                 {
  28.                         byte[] buf = new byte[1024];
  29.                         System.out.println("读取前,没有数据,阻塞...");
  30.                         int len = in.read(buf);
  31.                         System.out.println("读到数据了");
  32.                         String s = new String(buf,0,len);
  33.                         System.out.println(s);
  34.                         in.close();
  35.                 }
  36.                 catch (IOException e)
  37.                 {
  38.                         throw new RuntimeException("管道读取流失败");
  39.                 }
  40.         }
  41. }

  42. class Write implements Runnable
  43. {
  44.         private PipedOutputStream out;
  45.         Write(PipedOutputStream out)
  46.         {
  47.                 this.out = out;
  48.         }
  49.         public void run()
  50.         {
  51.                 try
  52.                 {
  53.                         System.out.println("开始写入数据,等待6秒...");
  54.                         Thread.sleep(6000);
  55.                         out.write("pipde 来了".getBytes());
  56.                         out.close();
  57.                 }
  58.                 catch (Exception e)
  59.                 {
  60.                         throw new RuntimeException("管道输出流失败");
  61.                 }
  62.         }
  63. }
复制代码
我优化后的代码
  1. package cn.study.file;

  2. import java.io.IOException;
  3. import java.io.PipedInputStream;
  4. import java.io.PipedOutputStream;

  5. public class PipedStreamDemo {

  6.         /**
  7.          * @param args
  8.          * @throws IOException
  9.          */
  10.         public static void main(String[] args) throws IOException {
  11.                 // TODO Auto-generated method stub

  12.                 final PipedInputStream pis = new PipedInputStream();
  13.                 final PipedOutputStream pos = new PipedOutputStream();
  14.                
  15.                 Thread read = new Thread(){
  16.                         public void run(){
  17.                                 try {
  18.                                         byte[] buf = new byte[1024];
  19.                                         System.out.println("读取前,没有数据,等待...");
  20.                                         int len = pis.read(buf);
  21.                                         System.out.println("读到数据,打印数据...");
  22.                                         String str = new String(buf,0,len);
  23.                                         System.out.println(str);
  24.                                         pis.close();
  25.                                 } catch (IOException e) {
  26.                                         // TODO Auto-generated catch block
  27.                                         e.printStackTrace();
  28.                                 }
  29.                         }
  30.                 };
  31.                
  32.                 Runnable write = new Runnable(){
  33.                         public void run(){
  34.                                 try {
  35.                                         System.out.println("开始写入数据,等待6秒后...");
  36.                                         Thread.sleep(6000);
  37.                                         pos.write("piped lai la".getBytes());
  38.                                         pos.close();
  39.                                 } catch (Exception e) {
  40.                                         // TODO Auto-generated catch block
  41.                                         e.printStackTrace();
  42.                                 }
  43.                         }
  44.                 };
  45.                 pis.connect(pos);
  46.                 read.start();
  47.                 new Thread(write).start();               
  48.         }
  49. }
复制代码
大家帮我看看这样会不会有什么缺陷






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2