extending thread class or implementing runnable interface. Which is better? : Java thread



extending thread class or implementing runnable interface. Which is better? : Java thread..

Answer / Ujjwal Kumar

Both extending the Thread class and implementing the Runnable interface are ways to create a new thread in Java, but they have some differences. Extending the Thread class makes your class a subclass of the Thread class automatically with access to its instance methods like start(), run(), sleep() etc. However, it can lead to code duplication if you want to create multiple threads having common functionality. On the other hand, implementing the Runnable interface allows for better reusability and encapsulation as you define your thread's behavior in the run() method and then pass that Runnable object to a new Thread instance. So, it is generally considered better to implement the Runnable interface when creating threads.

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More Core Java Interview Questions

Name four container classes.

1 Answers  


Is call by reference possible in java?

1 Answers  


what is the constructor and how many types of constructors are used in java?

1 Answers  


What are meta-annotations?

1 Answers   Cyient,


What are the advantages and disadvantages of reference counting in garbage collection?

1 Answers  


Difference between a process and a program?

1 Answers   Infosys,


What are access specifiers in java ?

1 Answers   Akamai Technologies,


why java does not support unsigned keyword?

2 Answers  


how cani read a command line argument?(usingfile object).

3 Answers  


How do you check if an arraylist is not empty?

1 Answers  


How to define a constant variable in Java?

1 Answers   TCS,


Mention a package that is used for linked list class in java.

1 Answers  


Categories