Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Edited by sciSpider Java v3.0

...

Code Block
bgColor#FFcccc
class BadStop implements Runnable {
  public void run() {
    try {
      Thread.currentThread().sleep(1000);
    } catch(InterruptedException ie) { // Not executed 
        System.out.println(""Performing cleanup""); 
    } finally { // Not executed
        System.out.println(""Closing resources""); 
    }       
    System.out.println(""Done!"");
  }
}

class Controller {
  public static void main(String[] args) {
    Thread t = new Thread(new BadStop());
    t.start();  
    t.interrupt(); // Artificially induce an InterruptedException
    t.stop();      // Force thread cancellation
  }
}

...

Code Block
bgColor#ccccff
class ControlledStop implements Runnable{
  protected volatile boolean done = false;
  public void run() {
    while(!done) {
      try {
        Thread.currentThread().sleep(1000);
      } catch(InterruptedException ie) { 
          System.out.println(""Interrupted Exception"");
          // Handle the exception 
      } finally { 
          System.out.println(""Closing resources""); 
          done = true; 
      }
    } 
    System.out.println(""Done!"");
  }

  protected void shutdown(){
    done = true;
  }
}

class Controller {
  public static void main(String[] args) throws InterruptedException {  
    ControlledStop c = new ControlledStop();
    Thread t = new Thread(c);
    t.start();
    t.interrupt();       // Artificially induce an InterruptedException
    Thread.sleep(1000);  // Wait for some time to allow the exception
                         // to be caught (demonstration only)
    c.shutdown();
  }
}

...

Code Block
bgColor#FFcccc
class StopSocket extends Thread {
  protected Socket s;
  protected volatile boolean done = false;
  public void run() { 
    while(!done) {
      try {
        s = new Socket("somehost""somehost", 25);
        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        String s = null;
        while((s = br.readLine()) != null) { 
          // Blocks until end of stream (null)
        }
        System.out.println(""Blocked, will not get executed until some data is received. "" + s);
      } catch (IOException ie) { 
          System.out.println(""IO Exception""); 
          // Handle the exception
      } finally {
          System.out.println(""Closing resources"");
          done = true;
      }
    }
  }  

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

class Controller {
  public static void main(String[] args) throws InterruptedException, IOException {
     StopSocket ss = new StopSocket();
     Thread t = new Thread(ss);
     t.start();
     Thread.sleep(1000); 
     ss.shutdown();
  }
}

...

Code Block
bgColor#ccccff
class StopSocket extends Thread {
  protected Socket s;
  public void run() { 
    try {
      s = new Socket("somehost""somehost", 25);
      BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
      String s = null;
      while((s = br.readLine()) != null) { 
        // Blocks until end of stream (null)
      }
      System.out.println(""Blocked, will not get executed until some data is received. "" + s);
    } catch (IOException ie) { 
        System.out.println(""IO Exception"");
        // Handle the exception 
    } finally {
        System.out.println(""Closing resources"");
        try {
          if(s != null)
            s.close();
        } catch (IOException e) { /* Forward to handler */ }
    }
  }

  public void shutdown() throws IOException {
    if(s != null)
      s.close();
  }
}

class Controller {
  public static void main(String[] args) throws InterruptedException, IOException {
    StopSocket ss = new StopSocket();
    Thread t = new Thread(ss);
    t.start();
    Thread.sleep(1000); 
    ss.shutdown();
  }
}

...

Search for vulnerabilities resulting from the violation of this rule on the CERT website.

References

Wiki Markup
\[[API 06|AA. Java References#API 06]\] Class Thread, method {{stop}}
\[[Darwin 04|AA. Java References#Darwin 04]\] 24.3 Stopping a Thread
\[[JDK7 08|AA. Java References#JDK7 08]\] Concurrency Utilities, More information: Java Thread Primitive Deprecation 
\[[JPL 05|AA. Java References#JPL 05]\] 14.12.1. Don't stop
\[[JavaThreads 04|AA. Java References#JavaThreads 04]\] 2.4 Two Approaches to Stopping a Thread

...

CON34-J. Avoid deadlock by requesting locks in the proper order            11. Concurrency (CON)            CON36-J. Always synchronize on the appropriate object