Let’s look at the following code, it’s working exactly.
final class DemoThread
{
public void temp()
{
new Thread(new Runnable()
{
public void run()
{
System.out.println( "Isn't it great ?" ) ;
}
} ) .start() ;
}
}
final public class Main
{
public static void main(String[] args)
{
new DemoThread().temp();
}
}
It works fine and displays the message Isn’t it great ? on the console. The only question here is that why the Runnable interface need not be implemented by the class DemoThread?
Runnableis being implemented by the anonymous inner class within thetempmethod:Given that an instance of
DemoThreadisn’t being passed into theThreadconstructor, whether or not it implementsRunnableis completely orthogonal to how the thread runs.Your code is a similar to this:
Does that make it clearer for you?