socket getting closed when killing the thread using it
<p>I am getting a telnet connection using apache's telnet client.Then I am trying to read the input stream from this connection in a different thread.The requirement is that if the inputstream.read() doesn't report back in a second,give up.So I am using the following code :</p>

<pre><code>       in = telnet.getInputStream();
       ExecutorService executor = Executors.newFixedThreadPool(1);
        //set the executor thread working
        Callable&lt;Integer&gt; task = new Callable&lt;Integer&gt;() {
            public Integer call() {
               try {
                   return in.read();
               } catch (IOException e) {
                   e.printStackTrace();
               }
               return null;
            }
         };

         Future&lt;Integer&gt; future = executor.submit(task);
         Integer result =null;
         try {
              result= future.get(1, TimeUnit.SECONDS); //timeout of 1 sec
          } catch (TimeoutException ex) {

          } catch (InterruptedException e) {
             // handle the interrupts
          } catch (ExecutionException e) {
             // handle other exceptions
          } finally {
              future.cancel(false);
              executor.shutdown();
          }
</code></pre>

<p>but when I do executor.shutdown() I end up closing the socket automatically,which I don't want.I have to call executor.shutdown() to keep the memory available.</p>

<p>Please help..</p>

<p><strong>Edit</strong> : I figured out that in case of timeout,thread reading the stream can never get complete as there is nothing available on stream,so it just waits. thats why I m doing all this stuff to timeout after 1 second.so I m using shutdownNow(),which removes socket closed error but gives fatal thread interruption exception,where do I catch it ? </p>

<p><strong>EDIT</strong>       this solved the problem </p>

<p><a href="http://stackoverflow.com/questions/4492273/catching-thread-exceptions-from-java-executorservice">Catching thread exceptions from Java ExecutorService</a></p>

<p>Thanks all for help</p>
