As we all know, to implement Java multi-threading Two ways A: is directly inherit the thread class, B: is to implement the Runnable interface.
First on the code:
A: The thread class is inherited directly,
public class ThreadDemo1 extends Thread {
public void Run () {
Thread.CurrentThread (). GetName () and This.getname () can all be used to get the name of the thread
SYSTEM.OUT.PRINTLN ("Thread Name:" +thread.currentthread (). GetName ());
}
public static void Main (string[] args) {
ThreadDemo1 T1 = new ThreadDemo1 ();//Create First thread
T1.start ();//Start thread
ThreadDemo1 t2 = new ThreadDemo1 ();//Create a second thread
T2.start ();//Start thread
}
}
B: is to implement the Runnable interface.
public class ThreadDemo2 implements Runnable {
public void Run () {
Thread.CurrentThread (). GetName () and This.getname () can all be used to get the name of the thread
SYSTEM.OUT.PRINTLN ("Thread Name:" +thread.currentthread (). GetName ());
}
public static void Main (string[] args) {
ThreadDemo2 td = New ThreadDemo2 ();//Instantiate a thread
thread T1 = new Thread (TD);//Create First thread
T1.start ();//Start thread
Thread t2 = new Thread (TD);//Create a second thread
T2.start ();//Start thread
}
}
OK, implementing the Runnable interface has the following notable benefits for inheriting the thread class:
(1) Suitable for multiple threads of the same program code to handle the same resource situation, the virtual CPU (thread) and the Code of the program, the data effectively separated, better reflect the object-oriented design ideas.
(2) can avoid limitations due to the single inheritance of Java. We often encounter the case that when we want to put a subclass of a class that has been inherited into a multi-threaded, because a class can not have two parent classes at the same time, so can not inherit the thread class, then this class can only adopt the way of implementing the Runnable interface.
(3) It is advantageous to the robustness of the program, the code can be shared by multiple threads, and the code and data are independent. When multiple threads execute code from an instance of the same class, they are said to share the same code. Multiple threads manipulate the same data, regardless of their code. When shared access is the same object, that is, they share the same data. When a thread is constructed, the required code and data are passed in through an object as a constructor argument, an instance of a class that implements the Runnable interface.
For a detailed example, see: http://yuelangyc.iteye.com/blog/1056393
These two methods compare:
The implementation of the Runnable interface has unparalleled advantages over the extended thread class. This approach is not only beneficial to the robustness of the program, so that the code can be shared by multiple threads, and the code and data resources are relatively independent, which is particularly suitable for multiple threads with the same code to handle the same resource situation. In this way, the thread, code and data resources are effectively separated, which embodies the idea of object-oriented programming. As a result, almost all multithreaded programs are done by implementing the Runnable interface.
In addition: in mind a small knowledge point:Thread.CurrentThread (). GetName () and This.getname ()
Java multithreading extends thread and implements Runnable