First, the realization method
1. Inherit the thread class
2. Implement Runnable interface
(i) Inherit the thread class
For classes that directly inherit the thread, the code's approximate framework is:
class extends thread{Method 1; Method 2; .... Public void run () {// other code ... } property 1; property 2; ...}
Let's look at a wrong example:
1 /**2 * Inherit the thread class and call the Run method directly3 */4 classHelloextendsThread {5 PrivateString name;6 7 PublicHello () {8 }9 Ten PublicHello (String name) { One This. Name =name; A } - - Public voidrun () { the for(inti = 0; I < 5; i++) { -SYSTEM.OUT.PRINTLN (name + "Run" +i); - } - } + - Public Static voidMain (string[] args) { +Hello H1 =NewHello ("A"); AHello H2 =NewHello ("B"); at H1.run (); - H2.run (); - } -}
We will find that these are executed sequentially, stating that our invocation method is not correct and that the start () method should be called.
Change the main function to the following code
Public Static void Main (string[] args) { new Hello ("A"); New Hello ("B"); H1.start (); H2.start (); }
The result of the operation may be:
A run 0
B Run 0
A Run 1
B Run 1
A Run 2
B Run 2
A Run 3
A run 4
B Run 3
B Run 4
Because of the need to use the CPU resources, so every time the results of the operation is basically different.
Note: Although we are calling the start () method here, it is actually called the body of the run () method.
So: Why can't we just call the run () method?
My understanding is that the operation of the thread requires the support of the local operating system.
(ii) Realization of the Runnable interface
The code outlines the following framework:
class Implements runnable{Method 1; Method 2; .... Public void run () {// other code ... } property 1; property 2; ...}
Let's look at an example (note and inherit the thread, the main function is different):
1 /**2 * Call the Runnable method3 */4 classHelloImplementsRunnable {5 PrivateString name;6 7 PublicHello () {8 }9 Ten PublicHello (String name) { One This. Name =name; A } - - Public voidrun () { the for(inti = 0; I < 5; i++) { -SYSTEM.OUT.PRINTLN (name + "Run" +i); - } - } + - Public Static voidMain (string[] args) { +Hello H1 =NewHello ("A"); AThread T1 =NewThread (H1); atHello H2 =NewHello ("B"); -Thread t2 =NewThread (H2); - T1.start (); - T2.start (); - } -}
Possible run results are:
A run 0
B Run 0
B Run 1
B Run 2
B Run 3
B Run 4
A Run 1
A Run 2
A Run 3
A run 4
The difference between thread and runnable:
If a class inherits the thread, it is not suitable for resource sharing. However, if the Runable interface is implemented, it is easy to realize the resource sharing.
To summarize:
The benefits of implementing the Runnable interface are more than inheriting the thread class:
1) Suitable for multiple threads of the same program code to process the same resource
2) can avoid the restriction of single inheritance in Java
3) Increase the robustness of the program, code can be shared by multiple threads, code and data Independent.
Therefore, I suggest that everyone try to implement the interface.
How to implement Multithreading in Java