如何关闭锁定的InputStream?

时间:2017-10-16 06:39:35

标签: java inputstream jtextfield

我正在努力通过jsch库将jtextfield组件的输入发送到其他linux机器。命令进展顺利,但程序没有正常关闭。

shell线程等待并且永远不会关闭。以下是该主题的状态。

"Shell for 192.168.0.101" #29 prio=5 os_prio=0 tid=0x00000000165cf000 nid=0x2df4 in Object.wait() [0x000000001ce3e000]
   java.lang.Thread.State: WAITING (on object monitor)
        at java.lang.Object.wait(Native Method)
        - waiting on <0x00000000c3c8c8e0> (a com.project.object.TextFieldStreamer)
        at java.lang.Object.wait(Object.java:502)
        at com.project.object.TextFieldStreamer.read(TextFieldStreamer.java:48)
        - locked <0x00000000c3c8c8e0> (a com.project.object.TextFieldStreamer)
        at java.io.InputStream.read(InputStream.java:170)
        at com.jcraft.jsch.ChannelSession.run(ChannelSession.java:245)
        at com.jcraft.jsch.ChannelShell.run(ChannelShell.java:34)
        at java.lang.Thread.run(Thread.java:745)

   Locked ownable synchronizers:
        - None

看起来好像在等待输入,但我想关闭程序。 我试过in.close(),但它不起作用。 我怎么能关闭呢?

public class TextFieldStreamer extends InputStream implements ActionListener
{
    public static final String TAG = "TextFieldStreamer";
    private JTextField         tf;
    private String             str = null;
    private int                pos = 0;

    public TextFieldStreamer(JTextField jtf)
    {
        tf = jtf;
    }

    // gets triggered everytime that "Enter" is pressed on the textfield
    @Override
    public void actionPerformed(ActionEvent e)
    {
        str = tf.getText() + "\n";
        pos = 0;
        tf.setText("");
        synchronized (this)
        {
            // maybe this should only notify() as multiple threads may
            // be waiting for input and they would now race for input
            this.notifyAll();
        }
    }

    @Override
    public int read()
    {
        // test if the available input has reached its end
        // and the EOS should be returned
        if (str != null && pos == str.length())
        {
            System.out.println("str ended up.");
            str = null;
            // this is supposed to return -1 on "end of stream"
            // but I'm having a hard time locating the constant
            return java.io.StreamTokenizer.TT_EOF;
        }
        Preferences.Log(TAG, "TextFieldStreamer.read(): " + "str: " + str);
        // no input available, block until more is available because that's
        // the behavior specified in the Javadocs
        while (str == null || pos >= str.length())
        {
            try
            {
                // according to the docs read() should block until new input is available
                synchronized (this)
                {
                    System.out.println("wait");
                    this.wait();
                }
            }
            catch (InterruptedException ex)
            {
                ex.printStackTrace();
            }
        }
        // read an additional character, return it and increment the index
        return str.charAt(pos++);
    }
}

1 个答案:

答案 0 :(得分:0)

在这种情况下,您可以通过不使用this作为锁定对象来解决问题。使用专用的Object lock = new Object()来使用synchronized