首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

管道源练习

2012-11-22 
管道流练习/* * 程序头部注释开始* 程序的版权和版本声明部分* Copyright (c) 2011, 烟台大学计算机学院学

管道流练习

/* * 程序头部注释开始   * 程序的版权和版本声明部分   * Copyright (c) 2011, 烟台大学计算机学院学生   * All rights reserved.   * 文件名称:管道流练习                        * 作    者:薛广晨                               * 完成日期:2012  年 11 月  11  日   * 版 本号:x1.0               * 对任务及求解方法的描述部分   * 输入描述:  * 问题描述: * 程序输出:   * 程序头部的注释结束 */import java.io.*;class Read implements Runnable{private PipedInputStream in;Read(PipedInputStream in){this.in = in;}public void run(){try{byte[] buf = new byte[1024];System.out.println("读取前。。没有数据阻塞");int len = in.read(buf);System.out.println("读到数据。。阻塞结束");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{System.out.println("开始写入数据,等待6秒后。");Thread.sleep(6000);out.write("piped lai la".getBytes());out.close();}catch (Exception e){throw new RuntimeException("管道输出流失败");}}}class PipedStreamTest {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();}}

热点排行