有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

当输入流中没有更多数据时,java程序挂起

我正在为国际象棋引擎(Stockfish)编写前端。引擎是用C++编写的,所以我的程序运行可执行的StfFISH。exe。然后,我需要向引擎提供命令行指令并检索其输出。我已经成功地从我发送的第一条指令(“uci”)获取输出,并将其打印到java系统输出。这很好,但是程序会挂起在读线上的while循环中,因为引擎的输出已经完成,并且没有遇到空字符串。所以打印“完成”的行永远不会被执行。 任何关于如何解决这一问题的想法都将不胜感激

      public static void main(String args[]) {
       String line = "";
        try {
            ProcessBuilder pb = new ProcessBuilder("d://Stockfish/stockfish-  7-win/Windows/stockfish7.exe");
            Process pr = pb.start();

            String[] cmd = new String[2];

            cmd[0] = "uci" ;  
            cmd[1] = "isready" ;

            PrintWriter writer = new PrintWriter(pr.getOutputStream()) ;

            InputStreamReader stream = new InputStreamReader(pr.getInputStream()) ;

            BufferedReader input = new BufferedReader(stream);

            writer.println(cmd[0]) ;  
            writer.flush() ;

            while ((line = input.readLine()) != null) {
                System.out.println(line);                   
                System.out.println("got to here") ;
            }

            System.out.println("done") ;



            writer.println(cmd[1]) ;  
            writer.flush() ;

共 (0) 个答案