Thread difference between inheritance and interfaces to achieve Runable

1.Thread class methods and implement Runnable way of contact:

JDKAPI Thread class is seen through the implement Runnable

2.Thread class methods and implement Runnable difference:

  • Inheritance Thread: threaded code stored Thread subclass run method.
  • Achieve Runnable: presence subclass run multithreaded code interface.
  • Realize different process steps, it requires a constructor parameter into a target implement Runnable

3.Thread class and the comparative implement Runnable:

  • Limitations achieve Runnable avoid single inheritance
  • Inheritance Thread: Use the same inheritance Thead class resources between multiple threads created is not shared, each thread's task is to separate existence
  • Implement Runnable interface: using the same Runnable class that implements the interface, multi-threaded resource creation is shared, multi-threaded service is the same task

Example 4. Comparison of two types of threads ways

  • Inheritance Thread

  • Implement Runnable:

Released nine original articles · won praise 0 · Views 213

Guess you like

Origin blog.csdn.net/mlh532354163/article/details/103341761