Java can implement multi-threading by inheriting the thread class, with the following steps:
- Defines a new class that inherits from the thread class.
- Implement the Run () method in the new class, which is the running program code for the new thread.
- Creates a new class object and invokes the object's start () method.
Thread is stored in the Java.lang class library, but does not need to load the Java.lang class library because it will load automatically. In addition, the run () method is a method defined in the thread class that writes the program code of the thread in the run () method, and in fact does the overridden operation. Here is an example of how to implement multithreading in this way:
public class threaddemo9_1{public static void Main (String args[]) { new Testthread (). Start (); Loop output for (int i = 0; i < i++) { System.out.println ("Main thread is running"); }}} Class Testthread extends thread{public void Run () {for (int i = 0; i <; i++) { System.ou T.println ("Testthread is Running");}}}
Compiling the Run program, you can see that the "main thread is running" and "Testthread is running" are alternately printed, indicating that the main () function and the run () function are running concurrently.
Java Multithreading Implementation 1, inheriting the thread class