Java - Threads - Interview Questions and Answers on Runnable Interface

Q1.  Different ways of implementing Threads in Java ?

Ans. Threads in Java can be implement either by Extending Thread class or implementing runnable interface.

Q2.  Is runnable a Marker interface ?

Ans. No , it has run method declared.

Q3. Name few classes that implement Runnable interface?

Ans.[FutureTask, TimerTask, ForkJoinWorkerThread, Thread]

Q4. Runnable is a / an ...

 a.Abstract Class
 b.Concrete Class


Q5.  What is the use of runnable interface if we can always create a new thread using Thread class ?

Ans. It's a choice to be made whether to use first approach ( Thread class ) or second approach ( runnable interface ) by the programmer. The second facility has been given for cases where your class is already extending some parent class and hence cannot extend another class ( for Thread ) as Java doesn't allow multiple inheritance.

Q6. Why do we need Thread class even in case we execute thread using runnable interface ?

Ans. Thread class holds the definition of start method ( This is the method that starts execution of new thread and then calls run method within the scope of new thread ). Interfaces don't hold any definition and so does runnable. So it makes it necessary the usage of Thread class , whatever implementation you choose.

When your class extends the thread class, it carries the definition of start method from parent Thread class onto itself and hence new yourClass.start() helps starting a new thread and then executing run method in that new thread scope.

When you implement runnable interface , you are just making it sure to the JVM that you have implemented the required method ( run() ) which the Thread start method will look for upon executing start method.