本帖最后由 bityoungs 于 2011-09-01 23:36:14 编辑

解决方案 »

  1.   


        public static void main(String[] args) {
            // TODO Auto-generated method stub
            TestInOut tio = new TestInOut();
            tio.send();
            Thread t = new Thread() {
                public void run(){
                   new MyTest().main(null);
                }
            }        t.start();        };    }
    这样做是干啥呢?
      

  2.   


    import java.io.*;
    import java.util.Scanner;public class TestInOut implements Runnable {
    //    Process p = null;
    //    public TestInOut() {
    // try {
    //    p = Runtime.getRuntime().exec("java MyTest");
    //     new Thread(this).start();
    // } catch (Exception e) {
    //     System.out.println(e.getMessage());
    // }
    //    }    public void send() {
    try {
        System.out.println("this is the thread : main");
       // OutputStream ops = p.getOutputStream();
       DataOutputStream dos = new DataOutputStream(System.out);
        while(true) {//为什么不用if而用while呢
    try {
        Thread.sleep(1000);
    } catch (Exception e) { }
    System.out.println(Thread.currentThread().getName()
    + " is Writing");
    // ops.write("Help\r\n".getBytes());
    dos.write("Help\r\n".getBytes());//不断的往控制台输出信息,不知道楼主为了什么?????   
        }
    } catch (Exception e) {
        System.out.println(e.getMessage());
    }
        }    public void run() {
    //InputStream in = p.getInputStream();
    //BufferedReader bfr = new BufferedReader(new InputStreamReader(in));
    Scanner r = new Scanner(System.in);
    System.out.println("this is the thread : TestInOut");
    while (true) {
        String strLine;
        try {
    //strLine = bfr.readLine();
    while(r.hasNext()) {//从控制台读到信息后输出
        strLine = r.nextLine();//再一次阻塞,如果send方法没有while用户输入信息后,回车两次(因为两次阻塞) 控制台才能输出信息, 
        System.out.println(Thread.currentThread().getName() + "::"
    + strLine);
    }
        } catch (Exception e) {
    e.printStackTrace();
        }
    }
        }    public static void main(String[] args) throws InterruptedException {
    TestInOut tio = new TestInOut();
    new Thread(tio).start();// 运行run函数,死循环
    tio.send(); //运行send函数,死循环
    new MyTest().main(args);//云新MyTest中的main函数,又是死循环,死等待,等待用户输入
        }
    }class MyTest {
        public static void main(String args[]) {
    while (true) {     
        try {
    System.out.println(Thread.currentThread().getName()
    + "::"
    + (new BufferedReader(new InputStreamReader(System.in))
    .readLine()));//这是一个阻塞式的函数,必须不断的往控制台输入信息,线程才能循环执行
        } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
        }
    }
        }
    }
    //
      

  3.   

    直接到TestInOut类的run方法里掉用class MyTest中的main不就行了、