With the release of Java 8, there is now a third option.
Runnable
is a functional interface, which means that instances of it can be created with lambda expressions or method references.
Your example can be replaced with:
new Thread(() -> { /* Code here */ }).start()
or if you want to use an ExecutorService
and a method reference:
executor.execute(runner::run)
These are not only much shorter than your examples, but also come with many of the advantages stated in other answers of using Runnable
over Thread
, such as single responsibility and using composition because you're not specializing the thread's behaviour. This way also avoids creating an extra class if all you need is a Runnable
as you do in your examples.