0

我正在创建这个程序来测试在聊天服务器程序的线程中获取用户输入。该程序在 停止read = into.readLine();。为什么会这样,发生了什么?

这是代码:

    import java.io.*;
    import java.net.*;

    public class ThreadClass implements Runnable
    {
        DataInputStream in;
        private boolean checkLoop = false;


    public void run()
    {
        BufferedReader into = new BufferedReader(new        InputStreamReader(System.in));
        String read;
        System.out.println("Welcome...");
        while(!checkLoop)
        {
            try
            {
                System.out.println("running1");
                read = into.readLine();
                System.out.println(read);
                if(read.equals(".bye"))
                {
                    checkLoop = true;
                }
                Thread.sleep(500);
            }
            catch(IOException e)
            {
                System.out.println(e);System.out.println("running2");
            }
            catch (InterruptedException ie)
            {
                System.out.println(ie);System.out.println("running3");
            }
        }
        System.out.println("running4");
    }

    public static void main(String[]args)
    {
        ThreadClass main = new ThreadClass();
        Thread t1 = new Thread(main);
        t1.start();
    }

    }
4

1 回答 1

0

当你使用“read = into.readLine();” 程序将停止并等待用户按下“Enter”键。从我的角度来看,您的程序运行良好。

尝试在控制台中输入一些内容,您将看到程序正常运行。

于 2015-07-19T22:49:41.200 回答