Thread and Runnable difference and contact

  • Way to create multi-threaded

  • Method 1: inherited from the Thread class

        1. Create a Thread class inheritance to subclasses
        2. Thread class override RUN () -> operation is declared in this thread of execution run () in
        3 to create an object subclass of the Thread class
        4. This object calls start ()

  • Second way: implement Runnable

       1. Create a class that implements Runnable interface
       2. implement the abstract class to implement the method of Runnable: RUN ()
       3. Create an object implementation class
       4. This object is passed to the constructor of the Thread class as a parameter, creating Thread object class
       5. Thread objects by calling the start () of class

  • Links and differences between the two approaches

       contact:

       public class Thread implements Runnable,Thread实现Runnable接口。

       Both approaches need to override the run (), a logical statement will be executed in the thread run () in.

       the difference:

Development: preference: implement Runnable manner
 . Reasons: 1 manner without the limitations of single inheritance class;
            embodiment 2 is more suitable to achieve a plurality of threads to handle the case where shared data.

Published 49 original articles · won praise 34 · views 6893

Guess you like

Origin blog.csdn.net/weixin_40391011/article/details/104084124