The multi-threaded Runnable interface and its advantages

Multithreading to achieve the Runnable interface:

1. Create a class that implements the interface Runnable
2. In the implementation class override the run method Runnable interface
3. Create a target Runnable interface implementation class
4. Create Thread class object constructor parameter passing as: Runnable interface implementation class of the object
5. Call the start method of the Thread class, multi-start threads

 

 

 

 

 Print the results (concurrent):

 

Implement Runnable interface to create multiple threads and inheritance Thread class compared to the advantages are:

1. Avoid the limitations of single inheritance Thread class inheritance.
[Class can only single inheritance, class inheritance Thread class can not inherit other classes
and implement the Runnable interface, you can inherit from other classes, implement other interfaces
]

2. Implement Runnable interface reduces the coupling thread object and thread tasks, enhances scalability of the program.
[Implement Runnable, and the set tasks and open a new thread thread separation (decoupling)
implementation class overrides the method to set the run-threaded task
to create a Thread class object, call the start method to open a new thread
creation Thread class object constructor implemented passed Runnable interface class object, can pass different implementation class (scalability)
]
3. implement Runnable thread individually encapsulated object, object-oriented thinking more in line.

Guess you like

Origin www.cnblogs.com/cdlyy/p/11564299.html