Java Threads Question:
Download Job Interview Questions and Answers PDF
How to create multithreaded program? Explain different ways of using thread? When a thread is created and started, what is its initial state?
Or
Extending Thread class or implementing Runnable Interface. Which is better?
Answer:
You have two ways to do so. First, making your class "extends" Thread class. The other way is making your class implement "Runnable" interface. The latter is more advantageous, cause when you are going for multiple inheritance, then only interface can help. . If you are already inheriting a different class, then you have to go for Runnable Interface. Otherwise you can extend Thread class. Also, if you are implementing interface, it means you have to implement all methods in the interface. Both Thread class and Runnable interface are provided for convenience and use them as per the requirement. But if you are not extending any class, better extend Thread class as it will save few lines of coding. Otherwise performance wise, there is no distinguishable difference. A thread is in the ready state after it has been created and started.
Download Java Threads Interview Questions And Answers
PDF
Previous Question | Next Question |
What is the difference between yielding and sleeping? | What is mutual exclusion? How can you take care of mutual exclusion using Java threads? |