There are two ways to implement multi-thread in Java: inheriting the Thread class or implements the runnable interface.
The Runnable interface is very simple, defining a method run (), inheriting runnable and implementing this
The method can implement multi-thread, but this Run () method cannot call himself, must be called by the system, otherwise it is nothing difference with other methods.
First give a simple example:
Public Class Multithread IMPLEMENTS Runnable {
Public static void main (String [] args) {
For (int i = 0; i <10; i ) {
New thread (New Multithread ()). start (); // Correct
// new thread (new multithread ()). Run 1
// new multithread (). Run () 2
}
}
Public void run () {
System.out.println (thread.currentthread (). Getname ());
}
}
10 new threads should be launched after running, plus 11 threads of the main thread, and should be output to be thread-1 know 10.
Here to pay attention to the way the starting thread is to call the Start () method of Thread () method, not a RUN method, if
1 or 2 input in the comment is 10 main, because this time the Run () method is restored, and the ordinary method is the same.
We can also give a conclusion: All threads are a Thread instance at runtime, although you can inherit thread
Implement a multi-thread, but ultimately a example of thread.