2017-10-16 20 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()을 사용하여 작업하십시오 synchronized