2017-10-16 163 views
0

我目前正在通過jsch庫將jtextfield組件的輸入發送到其他linux機器。命令順利,但程序沒有正常關閉。如何關閉鎖定的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()與​​

相關問題