Java uses the Thread class to represent threads, and all Thread objects must be instances of Thread or its subclasses. The purpose of each thread is to perform a certain task, which is essentially to execute a program stream (a sequence of code executed). Java uses the thread body to represent this program flow.
Create a Thread class by inheriting the Thread class
To create and start multithreading by inheriting the Thread class, do the following:
- Subclass Thread and override its run() method. The body of the run() method represents what the Thread needs to do. This is why the run() method is called the thread body.
- Creating an instance of the Thread subclass creates a Thread object.
- Call the start() method of the thread object to start the thread.
public class MyThread extends Thread { @Override public void run() { for (int i = 1; i <= 5; I ++) {system.out.println (thread.currentThread ().getName() + "" + I); } } public static void main(String[] args) { for (int i = 1; i <= 6; System.out.println(thread.currentThread ().getName() + "" + I); If (I == 3) {// create the first thread thread1 = new MyThread(); // Start the first thread thread1.start(); MyThread thread2 = new MyThread(); // Start the second thread thread2.start(); }}}} /* // Output result: main 1 main 2 main 3 main 4 main 5 main 6 Thread-1 1 Thread-0 1 Thread-1 2 Thread-0 2 Thread-1 3 Thread-0 3 Thread-1 4 Thread-0 4 thread-1 5 thread-0 5 */ / or /* / main 1 main 2 main 3 main 4 main 5 main 6 Thread-0 1 Thread-1 1 Thread-0 2 Thread-1 2 Thread-0 3 Thread-1 3 Thread-0 4 Thread-1 4 Thread-0 5 Thread-1 5 */Copy the code
The order of execution of the first thread and the second thread may not be the same for each run, depending on the thread scheduler of the VIRTUAL machine.
Implement the Runnable interface to create thread classes
The steps to implement the Runnable interface to create and start multithreading are as follows:
- Define the implementation class of the Runnable interface and override the run() method of that interface, whose method body is also the thread execution body of that thread.
- Create an instance of the Runnable implementation class and use that instance as the target of the Thread to create the Thread object, which is the actual Thread object.
public class MyThread implements Runnable { @Override public void run() { for (int i = 1; i <= 5; I ++) {system.out.println (thread.currentThread ().getName() + "" + I); } } public static void main(String[] args) { for (int i = 1; i <= 6; System.out.println(thread.currentThread ().getName() + "" + I); If (I == 3) {// create Runnable instances: MyThread MyThread MyThread MyThread = new MyThread(); Thread1 = new Thread(MyThread, "first Thread "); thread1 = new Thread(MyThread," first Thread "); // Start the first thread thread1.start(); Thread2 = new Thread(myThread, "second Thread "); // Start the second thread thread2.start(); }}}} /* // Output result: Main 1 main 2 main 3 main 4 main 5 main 6 first thread 1 second thread 1 first thread 2 second thread 2 first thread 3 second thread 3 first thread 4 second thread 4 first thread 5 second thread 5 */ // or /* // Main 1 main 2 main 3 main 4 main 5 main 6 Second thread 1 first thread 1 second thread 2 first thread 2 second thread 3 first thread 3 second thread 4 first thread 4 second thread 5 first thread 5 * /Copy the code