Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: code in 2nd NCCE/CS set is thread-safe

...

Code Block
bgColor#FFcccc
class StopSocket implements Runnable {
  private final Socket socket;
  private volatilefinal boolean done = falseBufferedReader in;
  
private  public void shutdown() {
    volatile boolean done = truefalse;
  }
  
  public void runStopSocket() { 
    trythrows IOException {
      this.socket = new Socket("somehost", 25);
    this.in = BufferedReader br = new BufferedReadernew BufferedReader(new InputStreamReader(this.socket.getInputStream()));
  }

  private final StringObject stringlock = new nullObject();
  
  public  while (!done && (string = br.readLine()) != null) { void run() {
    String string;
    try {
        // Blocks until end of stream (null)
synchronized (this.lock) {
        while (!this.done && }
(string = this.in.readLine()) != }null) catch{
 (IOException ie) { 
      // ForwardBlocks tountil handler
end of stream  }
(null)
        }
  
  
  public}
 static void main(String[] args) throws IOException, InterruptedException } catch (IOException ie) {
     StopSocket ss// =Forward new StopSocket();
to handler
    }
 Thread thread}

 = new Thread(ss);public void shutdown() {
    thread.start();this.done = true;
  }

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

Noncompliant Code Example (blocking IO, interruptible)

...

Code Block
bgColor#FFcccc
class StopSocket implements Runnable {
  private volatile Socket socket;
  // ...
  
  public void run() { 
    String string;
    try {
      socketsynchronized = new Socket("somehost", 25);
 (this.lock) {
     BufferedReader br = newwhile BufferedReader(new InputStreamReader(socket.getInputStream()));
      String string = null;
      while (!Thread.interrupted() && (string = br.(!Thread.interrupted() && (string = this.in.readLine()) != null) { 
          // Blocks until end of stream (null)
        }
      }
    } catch (IOException ie) { 
      // Forward to handler
    }
  }
  
  
  public static void main(String[] args) throws IOException, InterruptedException {
    StopSocket ss = new StopSocket();
    Thread thread = new Thread(ss);
    thread.start();
    Thread.sleep(1000); 
    thread.interrupt();
  }
}

...

Code Block
bgColor#ccccff
class StopSocket implements Runnable {
  private volatile Socket socket; // Socket needs to be shared 
  
  // ...
  
  public void shutdownrun() { throws
 IOException {
  String string;
 if (socket != null)try {
      socket.close();synchronized (this.lock) {
    }
  }
  
while ((string public void run(= this.in.readLine()) != null) { 
    try   {
   // Blocks until socketend =of newstream Socket("somehost", 25);(null)
      BufferedReader br =}
 new BufferedReader(new InputStreamReader(socket.getInputStream()));
   }
   String string = null; } catch (IOException ie) { 
      while ((string = br.readLine()) != null) { // Forward to handler
    } finally {
      try {
  // Blocks until end of stream shutdown(null);
      }
 catch   } catch (IOException ie(IOException e) {
  
      // Forward to handler
    } finally {}
    }
  try {}
  
  public   void shutdown();
 throws IOException {
   } catch (IOException e) { this.socket.close();
  }

  public static void main(String[] args)  // Handle the exception 
      }
    }
  }


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

...

Code Block
bgColor#ccccff
class StopSocket implements Runnable {

  private final publicSocketChannel void run() {
    while (!Thread.interrupted()) {
      trysc;
  
  public StopSocket() throws IOException {
    InetSocketAddress addr = new InetSocketAddress("somehost", 25);
    this.sc = SocketChannel.open(addr);    
  }
  
  private final Object lock = new Object;
  
  public void run() {
    ByteBuffer buf   InetSocketAddress addr = new InetSocketAddress("somehost", 25ByteBuffer.allocate(1024);
    try {
    SocketChannel sc =synchronized SocketChannel.open(addr);(this.lock) {
        ByteBuffer buf = ByteBuffer.allocate(1024);
while (!Thread.interrupted()) {
          this.sc.read(buf);
          // ...
        }
      }
    } catch (IOException ie) {
        // HandleForward theto exceptionhandler
      }
    }
  }


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

...