title: Java thread hanging when trying to interrupt and join main
body: <p>So I've been struggling with this problem for a whole day now, and having consulted some threading tutorials and examples am still failing to achieve my desired result.</p>
  
  <p>I have a Thread whose only job is to poll a LinkedBlockingQueue of Strings and then use a PrintWriter to deliver the Strings over a socket to a client. That functionality is working fine, but I am now trying to enhance it by allowing it to gracefully fail in the event of a connection interruption and restart. In order to accomplish this, I am calling interrupt on the thread, and then join, with the end goal of recreating the Thread object to start over. Unfortunately the Thread hangs on the call of join which must mean the Thread never actually manages to die, but I am totally bewildered as to why this might be. Relevant code below.</p>
  
  <pre><code>try {
      resultSetStreamer.interrupt();
      resultSetStreamer.join();
      logger.info("Streamer finished.");
  } catch (InterruptedException e) {}
  </code></pre>
  
  <p>Actual thread code;</p>
  
  <pre><code>class ResultSetStreamer implements Runnable {
  
      GZIPOutputStream gzos = null;
      Socket clientSocket = null;
  
      @Override
      public void run() {
  
          try {
              logger.debug("Thread started.");
  
              // Blocks and waits for an external connection.
              clientSocket = serverSocket.accept();
  
              // Creates a compression stream using best possible compression
              // to the external connection.
              gzos = new GZIPOutputStream(clientSocket.getOutputStream()) {
                  {
                      def.setLevel(compression);
                  }
              };
              PrintWriter toClient = new PrintWriter(new BufferedWriter(
                      new OutputStreamWriter(gzos), bufferSize), false);
  
              while (true) {
                  if (Thread.interrupted()) {
                      throw new InterruptedException();
                  }
                  if (moreRowsToReceive || !dataBuffer.isEmpty()) {
                      // Synchronisation point.
                      String row = dataBuffer.poll(pollTime,
                              TimeUnit.MILLISECONDS);
                      if (row != null) {
                          toClient.println(row);
                          logger.trace("Current row: " + ++currentCount + ".");
                      }
                  } else {
                      toClient.flush();
                      break;
                  }
              }
          } catch (IOException e) {
              e.printStackTrace();
          } catch (InterruptedException e) {
              e.printStackTrace();
          } finally {
              try {
                  gzos.finish();
                  clientSocket.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
          logger.debug("Thread finished.");
      }
  }
  </code></pre>
  
  <p>Main place to look at is the while(true) loop, which should always loop around and check if the Thread has been interrupted, in which case it throws the exception which is caught at the bottom in order to allow the Thread to die. I don't think the PrintWriter should be blocking the execution of code. Any help would be much appreciated.</p>