Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

This noncompliant code example explicitly invokes run() in the context of the current thread.

Code Block
bgColor#FFCCCC
public final class Foo implements Runnable {
  public void run() {
    // ...
  }
  
  public static void main(String[] args) {
    Foo f = new foo();
    new Thread(f).run();
  }
}

...

This compliant solution correctly uses the start() method to start a new thread. The start() method internally invokes the run() method in the new thread.

Code Block
bgColor#ccccff
public final class Foo implements Runnable {
  public void run() {
    // ...
  }
  
  public static void main(String[] args) {
    Foo f = new foo();
    new Thread(f).start();
  }
}

...