Thread series: two common methods of java multithreading

How to implement java multithreading? There are two common methods:
1) Integrate the Thread class and override the run() method
2) Implement the Runnable interface and the run() method of the interface

1, Integrate the Thread class and override the run() method

Start a new Thread by calling the start() method of Thread class and execute the run() method. The run() method provided in Thread is an empty method, which can be overridden for customization.

public class UP1013 {

    public static void main(String[] args) {
	myThread thread = new myThread();
	thread.start();
    }

}
class myThread extends Thread{
    
}

Operation results:

Thread. Start() -- > executes the run() method, but myThread does not have this method, then it calls the run() method of the parent Thread, which is an empty method. Next, rewrite run()

public class UP1013 {

    public static void main(String[] args) {
	myThread thread = new myThread();
	thread.start();
    }

}

class myThread extends Thread{
    public void run() {
	System.out.println("myThread run body!");
    }
}

Operation results:

myThread run body!

Although the method that inherits the Thread class is listed as a multi-threaded implementation, Thread is essentially an instance that implements the Runnable interface, which represents an instance of a Thread.

2, Implement the Runnable interface and the run() method of the interface

  • Customize the class and implement the Runnable interface, and implement the run() method of the interface
  • Create a Thread object and instantiate the Thread object with the object implementing the Runnable interface as a parameter.
  • Call the start() method of the Thread object.
public class UP1013 {

    public static void main(String[] args) {
	Thread thread  = new Thread(new myThreadR());
	thread.start();
    }
}

class myThreadR implements Runnable{
    @Override
    public void run() {
	System.out.println("myThreadR implements Runnable run body!");	
    }
}

No matter which of the above methods is used to realize multithreading, the Thread is finally operated through the API of Thread object.

Extension: can a class inherit Thread and implement Runnable interface at the same time? sure

Example 1:

public class UP1013  extends Thread implements Runnable {

    public static void main(String[] args) {
	System.out.println("OK");
	new UP1013().start();
    }

}

Operation results:

OK

UP1013 class implements the Runnable interface, but does not implement the run() method. Class UP1013 inherits the run() method in the Thread class and can be used as an implementation of the run() method of the Runnable interface.

Example 2: override the run() method

public class UP1013  extends Thread implements Runnable {
    
    public void run() {
	System.out.println("extends Thread implements Runnable !");
    }

    public static void main(String[] args) {
	System.out.println("OK");
	new UP1013().start();
    }

}

Operation results:

OK
extends Thread implements Runnable !

Tags: Multithreading

Posted by phpDVWeaver on Thu, 12 May 2022 08:56:14 +0300