请哪位仁兄帮忙解决一道多线程与输入输出的综合问题:编写一个java程序,有两个线程,一个负责输入读取,另一个负责输出。因为比较急用(2天内),我先谢谢大家。

解决方案 »

  1.   

    public interface BlockingQueue<E>extends Queue<E>支持两个附加操作的 Queue,这两个操作是:检索元素时等待队列变为非空,以及存储元素时等待空间变得可用。 BlockingQueue 不接受 null 元素。试图 add、put 或 offer 一个 null 元素时,某些实现会抛出 NullPointerException。null 被用作指示 poll 操作失败的警戒值。 BlockingQueue 可以是限定容量的。它在任意给定时间都可以有一个 remainingCapacity,超出此容量,便无法无阻塞地 put 额外的元素。没有任何内部容量约束的 BlockingQueue 总是报告 Integer.MAX_VALUE 的剩余容量。 BlockingQueue 实现主要用于生产者-使用者队列,但它另外还支持 Collection 接口。因此,举例来说,使用 remove(x) 从队列中移除任意一个元素是有可能的。然而,这种操作通常不 会有效执行,只能有计划地偶尔使用,比如在取消排队信息时。 BlockingQueue 实现是线程安全的。所有排队方法都可以使用内部锁定或其他形式的并发控制来自动达到它们的目的。然而,大量的 Collection 操作(addAll、containsAll、retainAll 和 removeAll)没有 必要自动执行,除非在实现中特别说明。因此,举例来说,在只添加了 c 中的一些元素后,addAll(c) 有可能失败(抛出一个异常)。 BlockingQueue 实质上不 支持使用任何一种“close”或“shutdown”操作来指示不再添加任何项。这种功能的需求和使用有依赖于实现的倾向。例如,一种常用的策略是:对于生产者,插入特殊的 end-of-stream 或 poison 对象,并根据使用者获取这些对象的时间来对它们进行解释。 以下是基于典型的生产者-使用者场景的一个用例。注意,BlockingQueue 可以安全地与多个生产者和多个使用者一起使用。  class Producer implements Runnable {
       private final BlockingQueue queue;
       Producer(BlockingQueue q) { queue = q; }
       public void run() {
         try {
           while(true) { queue.put(produce()); }
         } catch (InterruptedException ex) { ... handle ...}
       }
       Object produce() { ... }
     } class Consumer implements Runnable {
       private final BlockingQueue queue;
       Consumer(BlockingQueue q) { queue = q; }
       public void run() {
         try {
           while(true) { consume(queue.take()); }
         } catch (InterruptedException ex) { ... handle ...}
       }
       void consume(Object x) { ... }
     } class Setup {
       void main() {
         BlockingQueue q = new SomeQueueImplementation();
         Producer p = new Producer(q);
         Consumer c1 = new Consumer(q);
         Consumer c2 = new Consumer(q);
         new Thread(p).start();
         new Thread(c1).start();
         new Thread(c2).start();
       }
     }
      

  2.   

    可以用PipedInputStream 和PipedOutputStream 来实现吧
      

  3.   

    求找异常import java.io.BufferedReader;
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.io.OutputStreamWriter;
    import java.io.PrintWriter;
    import java.io.UnsupportedEncodingException;
    import java.util.Stack;public class ThreadHome {
    public static Stack<String> buf;
    public static void main(String[] args) {
    //读写线程
    ReaderThread rt=new ReaderThread();
    WriterThread wt=new WriterThread();

    Thread t1=new Thread(rt);
    Thread t2=new Thread(wt);
    //start
    //start read

    t1.start();
    //start write
    t2.start();
    }}
    class ReaderThread implements Runnable{
    @Override
    public void run() {
    String str=CharUtils.readLine();
    while(true){
    if(!str.equals("exit")){
    ThreadHome.buf.push(str);
    System.out.println("reading....");
    str=CharUtils.readLine();
    }
    else{//否则关闭输入流
    ThreadHome.buf.push(str);
    CharUtils.closeInputStream();
    }

    }

    }

    class WriterThread implements Runnable{
    @Override
    public void run() {
    String str=ThreadHome.buf.pop();
    while(true){
    if(!str.equals("exit")){
    CharUtils.writeLine(str);
    }
    else{
    CharUtils.closeOutputStream();
    }
    }
    }
    }
    class CharUtils{
    private static BufferedReader br=null;
    private static PrintWriter pw=null;
    {
    System.out.println("hello");
    try {
    br=new BufferedReader(new InputStreamReader(System.in,"utf-8"));
    } catch (UnsupportedEncodingException e) {
    System.out.println("BufferedReader Exception");
    }
    try {
    pw=new PrintWriter(new OutputStreamWriter(new FileOutputStream("src/homework.log",true)));
    } catch (FileNotFoundException e) {
    System.out.println("PrintWriter Exception");
    }

    }

    public static String readLine(){
    String str="";
    try {
    str=br.readLine();
    } catch (IOException e) {
    System.out.println("BufferedReader ReadLine Exception");
    }
    return str;
    }
    public static void writeLine(String str){
    pw.println(str);
    }
    //close inputstream
    public static void closeInputStream(){
    try {
    br.close();
    } catch (IOException e) {
    System.out.println("BufferedReader close Exception");
    }
    }
    //close outputstream
    public static void closeOutputStream(){
    pw.close();
    }

    }