Java multi-threaded relationship with the concurrent -8.3 Thread and Runnable

Thread is a class, and is a Runnable interface. Thread implements Runnable interface. There is only one Runnable abstract run () method, which means that Rnnable this interface does not have the characteristics of a multi-threaded, it is dependent Thread inside the start () method to create a child thread, then call again in this sub-thread inside thread realization of a good run () method, to perform the appropriate business logic to make the thread class has characteristics of a multi-threaded.

Next, Thread and Runnable are doing test
1, Thread test
to create a thread class
Here Insert Picture Description
and then create a test class:
Here Insert Picture Description
2, test Runnable
Runnable not start () method, how it implements multithreading it?
There are only Thread start () method, while the Thread class source which has a constructor that can pass a subclass of Runnable instance, that we can achieve multi-threaded Thread class, Runnable initiated by.
Here Insert Picture Description
Create a class MyRunnable, implement Runnable,
Here Insert Picture Description
and then create a test class to call MyRunnable:
Here Insert Picture Description
Relationship Thread and Runnable of:
(1) Thread is a class that implements the Runnable interface, through the start, to run the method Runnable assignment multithreaded characteristics;
(2) due to a single principle inherited Java classes, in the actual development process, in order to improve system scalability, often recommended by service class implements Runnable interface, business logic is encapsulated in the run method, the easy to follow to general category conferred on the characteristics of multi-threaded.

Guess you like

Origin blog.csdn.net/tanwenfang/article/details/92393780