Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: fixing code for compilation

...

Given a Thread object that has been constructed with a runnable argument, when invoking the Thread.run() method, the Thread object may be cast to Runnable to eliminate analyzer diagnostics.

Code Block
bgColor#ccccff
public void sampleRunTest(){

  Thread thread = new Thread(new Runnable() {
    @Override public void run() {
        // ...
      }
    });
  
  ((Runnable) thread).run();  // Exception: This does not start a new thread

}

Casting a thread to Runnable before calling the run() method documents that the explicit call to Thread.run() is intentional. Adding an explanatory comment alongside the invocation is highly recommended.

...