Method 1: Inherit the Thread class and overwrite the run () method. In the subclass of the created Thread class, rewrite run () and add the code to be executed by the thread. The following is an example: Public class mythread extends thread {
Int COUNT = 1, number;
Public mythread (INT num ){
Number = num;
System. Out. println ("creation thread" + number );
}
Public void run (){
While (true ){
System. Out. println ("Thread" + number + ": Count" + count );
If (++ COUNT = 6) return;
}
}
Public static void main (string ARGs []) {
For (INT I = 0; I <5; I ++) New mythread (I + 1). Start ();
}
}
This method is simple and clear, and conforms to everyone's habits. However, it also has a major drawback, that is, if our class has been inherited from a class (for example, the applet must inherit from the Applet Class), we cannot inherit from the Thread class. If we do not want to create a new class, what should I do?
We may wish to explore a new method: instead of creating a subclass of the thread class, we can directly use it, so we can only pass our method as a parameter to the Thread class instance, similar to callback functions. But Java has no pointer. We can only pass an instance of the class containing this method. So how can we limit that this class must contain this method? Of course, the interface is used! (Although abstract classes can also be satisfied, but inheritance is required. Why should we use this new method to avoid the limitations of inheritance ?)
Java provides the java. Lang. runnable interface to support this method.
Method 2: implement the runnable interface
The runnable interface has only one method run (). We declare our class to implement the runnable interface and provide this method. By writing our thread code into it, this part of the task is completed. However, the runnable interface does not support any threads. We must also create an instance of the thread class, which is achieved through the Thread class constructor public thread (runnable target. The following is an example:
Public class mythread implements runnable {
Int COUNT = 1, number;
Public mythread (INT num ){
Number = num;
System. Out. println ("creation thread" + number );
}
Public void run (){
While (true ){
System. Out. println ("Thread" + number + ": Count" + count );
If (++ COUNT = 6) return;
}
}
Public static void main (string ARGs []) {
For (INT I = 0; I <5; I ++) New thread (New mythread (I + 1). Start ();
}
}
Strictly speaking, creating a thread subclass instance is also feasible, but it must be noted that this subclass must not overwrite the Thread class's run method, otherwise, the thread will execute the run method of the subclass, rather than the run method of the class we use to implement the runnable interface. You may try this out.
Using the runnable interface to implement multithreading enables us to include all the code in a class and facilitate encapsulation. Its disadvantage is that we can only use one set of code, if you want to create multiple threads and run different code in each thread, you must create additional classes, in most cases, it may be better to use multiple classes to inherit the thread.