title: How To Close Locked InputStream?
body: <p>I'm currently working on sending input from jtextfield component to other linux machine by jsch library. Commands goes well but the program doesn't close properly.</p>
  
  <p>The shell thread waits and is never closed. Below is the status of the thread.</p>
  
  <pre><code>"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 &lt;0x00000000c3c8c8e0&gt; (a com.project.object.TextFieldStreamer)
          at java.lang.Object.wait(Object.java:502)
          at com.project.object.TextFieldStreamer.read(TextFieldStreamer.java:48)
          - locked &lt;0x00000000c3c8c8e0&gt; (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
  </code></pre>
  
  <p>It looks like waiting for input, but I want to close the program. 
  I tried in.close(), but it doesn't work.
  How can I close it?</p>
  
  <pre><code>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 &amp;&amp; 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 &gt;= 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++);
      }
  }
  </code></pre>