BufferedReader死锁:如何正确关闭BufferedReader

时间:2018-08-21 11:19:50

标签: java bufferedreader processbuilder

我遇到一个死锁问题,该问题是 BufferedReader InputStreamReader 读取的(从 InputStream 读取的) )。

有一个Swing GUI正在被用户关闭。正在废弃JFrame,并调用finish方法。

 DppGUI gui = this;
 addWindowListener(new WindowAdapter()
    {
        @Override
        public void windowClosing(WindowEvent windowEvent)
        {
            int choice = Dialogs.displayExitQuestionDialog(gui);
            if (choice == JOptionPane.YES_OPTION)
            {
                _dataReciever.finish();
                dispose();
            }
        }
    });

需要运行finish方法来中断while循环,以最终使线程完成。这是完成方法。

public void finish()
{
    _isRunning = false; // This boolean shall break a while loop.
    _listener = null;
    try
    {
        _processInputStream.close();
        System.out.println("Closed input stream");

        _inputStreamReader.close();
        System.out.println("Closed input stream reader");

        _reader.close();
        System.out.println("Closed reader");
    }
    catch (IOException e)
    {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

问题是我无法弄清楚如何从“外部”关闭BufferedReader以便行

while ((line = _reader.readLine()) != null)

被跳过。

这是死锁的线程(用户关闭gui后需要完成)。

@Override
public void run()
{
    _isRunning = true;
    CommandManager commandManager = new CommandManager();
    Process process = null;
    try
    {
        process = commandManager.makeCommand(MqttCoammands.MOSQUITTO_SUB,
                "HFC_001", "");
        System.out.println("Mosquitto Sub Command generated.");
        _processInputStream = process.getInputStream();
        _inputStreamReader = new InputStreamReader(_processInputStream);
        _reader = new BufferedReader(_inputStreamReader);

        String line = null;
        while (_isRunning)
        {
            while ((line = _reader.readLine()) != null) // The program deadlocks in this line when the GUI is closed
            {
                String[] splits = line.split(";");
                _parameterList = Arrays.asList(splits);
                System.out.println(_parameterList.toString());
                _listener.get(_parameterList);
            }
        }
        System.out.println("Left the While loop. Preparing end of Thread.");
        _listener = null;
        //            finish();
    }
    catch (IllegalCommandException e1)
    {
        e1.printStackTrace();
    }
    catch (UnsupportedOperatingSystemException e1)
    {
        e1.printStackTrace();
    }
    catch (IOException e1)
    {
        e1.printStackTrace();
    }
}

要关闭BufferedReader,我尝试将其设置为null。我尝试关闭BufferedReader,InputStreamReader和InputStream(如您在finish方法中看到的那样),但是没有任何效果,程序仍然死锁。有人遇到同样的问题吗?有人看到解决方案了吗?

谢谢!

编辑: 我忘了提到死锁之前的最后一个控制台输出是

  

封闭的输入流

它来自完成方法。这意味着InputStreamReader和BufferedReader没有关闭。

2 个答案:

答案 0 :(得分:0)

替换

    while (_isRunning)
    {
        while ((line = _reader.readLine()) != null) // The program deadlocks in this line when the GUI is closed
        {

使用

    while (_isRunning && (line = _reader.readLine()) != null)
    {

答案 1 :(得分:0)

  1. 在围绕输入流/阅读器创建包装器时-在包装器上执行关闭操作会在被包装的对象上关闭。因此,您不需要所有这些close调用。

  2. 我建议您停止自动关闭流的过程,并从null收到reader.readLine