Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: added interruptible channel CS

...

  • As a remediation measure, catching the ThreadDeath exception on the other hand can itself ensnarl multithreaded code. For one, the exception can be thrown anywhere, making it difficult to trace and effectively recover from the exceptional condition. Also, there is nothing stopping a thread from throwing another ThreadDeath exception while recovery is in progress.

Noncompliant Code Example (Thread.stop())

This noncompliant code example shows a thread that forcefully comes to a halt when the Thread.stop() method is invoked. Neither the catch nor the finally block executes. Any monitors that are held are immediately released, leaving the object in a delicate state.

...

Remove the default permission java.lang.RuntimePermission stopThread from the security policy file to deny the Thread.stop() invoking code, the required privileges.

Noncompliant Code Example (blocking IO)

This noncompliant code example uses the advice suggested in the previous compliant solution. However, this does not help in terminating the thread because it is blocked on some network IO as a consequence of using the readLine() method.

...

A Socket connection is not affected by the InterruptedException that results with the use of the Thread.interrupt() method. The boolean flag solution does not work in such cases.

Compliant Solution (close socket connection)

This compliant solution closes the socket connection, both using the shutdown() method as well as the finally block. As a result, the thread is bound to stop due to a SocketException. Note that there is no way to keep the connection alive if the thread is to be cleanly halted immediately.

...

A boolean flag can be used (as described earlier) if additional clean-up operations need to be performed.

Compliant Solution (2) (interruptible channel)

This compliant solution uses an interruptible channel, SocketChannel instead of a Socket connection. If the thread performing the network IO is interrupted using the Thread.interrupt() method, for instance, while reading the data, the thread receives a ClosedByInterruptException and the channel is closed immediately. The thread's interrupt status is also set.

Code Block
bgColor#ccccff

class StopSocket extends Thread {
  private volatile boolean done = false;
  public void run() { 
    while(!done) {
      try {
    	InetSocketAddress addr = new InetSocketAddress("somehost", 25);
        SocketChannel sc = SocketChannel.open(addr);
        ByteBuffer buf = ByteBuffer.allocate(1024);
        sc.read(buf);
        // ...
      } catch (IOException ie) {  
          // Handle the exception
      } finally {
          System.out.println("Closing resources");
          done = true;
      }
    }
  }  

  public void shutdown() throws IOException {
    done = true;
  }
}

Risk Assessment

Trying to force thread shutdown can result in inconsistent object state and corrupt the object. Critical resources may also leak if cleanup operations are not carried out as required.

...