Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Edited by NavBot

...

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();
  }
}

...

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