Saturday 29 June 2019

Best approach to define a Thread

There are 2 approached to define a Thread
  1. By extending Thread class.
  2. By implementing Runnable interface.
  • Among the 2 ways of defining a Thread, implements Runnable approach is always recommended.
  • In the 1st approach our class should always extends Thread class there is no chance of extending any other class hence we are missing the benefits of inheritance.
  • But in the 2nd approach while implementing Runnable interface we can extend some other class also. Hence implements Runnable mechanism is recommended to define a Thread.
One approach to define a Thread(not recommended to use): 

Output:
main method
run method
      or
main method
run method


Thanks..!!

No comments:

Post a Comment