2017-10-16 17 views
0

私は現在jtextのコンポーネントから他のLinuxマシンへの入力をjschライブラリで送信しています。コマンドは正常に機能しますが、プログラムが正常に終了しません。ロックされたInputStreamを閉じるには?

シェルスレッドは待機し、決して閉じません。以下はスレッドの状態です。

"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++); 
    } 
} 
+2

私は 'InputStream'をサブクラス化することが極限だと思います。私がそれを拡張する唯一の時間は、例えば、デコードや解読のように読む前に、バイトに対して行われた特定の作業が必要な場合です。テキストフィールドのロジックを別のクラスに移動し、ビルドされた 'InputStream'を使用することで、これをもう少し考え直すことができます。しかし、クールなプロジェクト! – MeetTitan

答えて

0

この場合、ロックオブジェクトとしてthisを使用しないことでこの問題を解決できます。専用のObject lock = new Object()を使用してください。​​

関連する問題