Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/395.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 关闭从inputStream读取时被阻止的线程_Java_Multithreading - Fatal编程技术网

Java 关闭从inputStream读取时被阻止的线程

Java 关闭从inputStream读取时被阻止的线程,java,multithreading,Java,Multithreading,这是代码 private static class NgrokRunner implements Runnable { private InputStream inputStream; private boolean doStop = false; public NgrokRunner(InputStream inputStream) { this.inputStream = inputStream; } @Override

这是代码

private static class NgrokRunner implements Runnable {
    private InputStream inputStream;

    private boolean doStop = false;

    public NgrokRunner(InputStream inputStream) {
        this.inputStream = inputStream;
    }

    @Override
    public void run() {
        // TODO Auto-generated method stub
        BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));

        String line;

        try {
            while((line = reader.readLine()) != null) {
                System.out.println(line);

                if (keepRunning()) {
                    continue;
                } else {
                    System.out.println("break ----");
                    break;
                }
            }
        } catch (Exception e) {
            //TODO: handle exception
            System.out.println("Ngrok exception");
        }
    }

    public synchronized void doStop() {
        this.doStop = true;
    }

    private synchronized boolean keepRunning() {
        return this.doStop == false;
    }
}
我是这样开始的

    ProcessBuilder processBuilder = new ProcessBuilder();
    processBuilder.command("ngrok", "http","8080", "--log=stdout");

    try {
        Process process = processBuilder.start();
        NgrokRunner runner = new NgrokRunner(process.getInputStream());
        Thread ngrokThread = new Thread(runner);
        ngrokThread.start();                                            
        for (int i = 0; i < 4; i++) {
            System.out.println(i);
            Thread.sleep(10L * 100L);
        }
        //System.out.println("It works");
        runner.doStop();

    } catch (Exception e) {
        //TODO: handle exception                                                    System.out.println(e);
    }
ProcessBuilder ProcessBuilder=new ProcessBuilder();
命令(“ngrok”、“http”、“8080”、“--log=stdout”);
试一试{
Process=processBuilder.start();
NgrokRunner=新的NgrokRunner(process.getInputStream());
螺纹ngrokThread=新螺纹(流道);
ngrokThread.start();
对于(int i=0;i<4;i++){
系统输出打印LN(i);
线程睡眠(10L*100L);
}
//System.out.println(“它工作”);
runner.doStop();
}捕获(例外e){
//TODO:处理异常系统.out.println(e);
}
但在while循环中,我的子线程正在从ngrok读取输入,阻塞,甚至在调用
doStop()
之后,它从未达到
if
条件,在该条件下,我正在检查bool标志以退出线程

那么,有谁能给我提出实现我理想状态的逻辑吗


我想要的是“通过线程中的ngrok二进制文件运行ngrok服务器,并在我需要时关闭线程/ngrok(如用户需要通过暂停/结束按钮)”

确定,所以我解决了它,这是最终的解决方案

run()代码:

现在从主线程开始

try {
       process = processBuilder.start();
       NgrokRunner runner = new NgrokRunner(process.getInputStream());
       ngrokThread = new Thread(runner);
       ngrokThread.start();

       for (int i = 0; i < 8; i++) {
           System.out.println(i);
           Thread.sleep(1000);
       }
       ngrokThread.interrupt();
} catch (Exception e) {
        //TODO: handle exception
        System.out.println(e);
}
试试看{
process=processBuilder.start();
NgrokRunner=新的NgrokRunner(process.getInputStream());
ngrokThread=新螺纹(流道);
ngrokThread.start();
对于(int i=0;i<8;i++){
系统输出打印LN(i);
睡眠(1000);
}
ngrokThread.interrupt();
}捕获(例外e){
//TODO:处理异常
系统输出打印ln(e);
}

不要接受异常(
catch(Exception e){}
),这是一种非常糟糕的做法,它只会隐藏错误,而不是解决错误,这只会使我们无法找出事情不起作用的原因。对不起,你是什么意思?我刚刚启动java:)您至少应该在错误/异常发生时打印出来:
catch(Exception e){e.printStackTrace();}
。捕获泛型异常是另一个我不会涉及的问题,只是不要忽略错误。好的,我会的,谢谢
try {
       process = processBuilder.start();
       NgrokRunner runner = new NgrokRunner(process.getInputStream());
       ngrokThread = new Thread(runner);
       ngrokThread.start();

       for (int i = 0; i < 8; i++) {
           System.out.println(i);
           Thread.sleep(1000);
       }
       ngrokThread.interrupt();
} catch (Exception e) {
        //TODO: handle exception
        System.out.println(e);
}