Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: the usual edits, promoting to draft

Thread startup It is critical to ensure that threads are started correctly. Thread start-up can be misleading because sometimes the code appears to be performing the function correctly, when in fact it may be executing in the wrong thread. It is critical to ensure that threads are started correctly.

The Thread.start() method starts executing a thread's run() method in that the respective thread. It is a mistake to directly invoke the run() method on a Thread object. When invoked directly, the statements in the run() method execute in the current thread instead of the newly created thread. Furthermore, if the Thread object is not constructed from a Runnable object but rather by instantiating a subclass of Thread that does not override the run() method, then a call to the subclass's run() method invokes Thread.run() which performs a no-operation.

...

Casting a thread to a Runnable before calling run() serves to document the intention of behind explicitly calling Thread.run(). Adding a disclaimer alongside the invocation is highly recommended.

...