Java Thread -- > inherit Thread and Runnable interfaces and call Thread instance object to realize multithreading

Posted by willwill100 on Fri, 04 Mar 2022 17:35:29 +0100

1. Inherit Thread class to realize multithreading

Java provides a Java Lang. Thread program class. If a class inherits this class, it means that this class is the main class of the Thread. However, it does not mean that this class can directly implement multithreading, because it also needs to override a run() method provided in the Thread class, which belongs to the main method of the Thread.
For example:

class MyThread extends Thread { //Body class of thread
	private String title;

	public MyThread(String title) {
    	this.title = title;
	}

	@Override
	public void run() { //Main method of thread
    	for (int x = 0; x < 10; x++) {
        	System.out.println(this.title + "function, x = " + x);
    	}
	}
}

The functions to be performed by multithreading should be defined in the run() method. It should be noted that under normal circumstances, if you want to use a method in a class, you must generate an instantiated object and then call the method provided in the class. However, the run() method cannot be called directly because it involves a system resource scheduling problem. Therefore, if you want to start multiple threads, you must use the start() method in the Thread class.
For example, if there is no start() method, use the run() method directly

class MyThread extends Thread { //Body class of thread
	private String title;

	public MyThread(String title) {
    	this.title = title;
	}

	@Override
	public void run() { //Thread theme method
    	for (int x = 0; x < 10; x++) {
        	System.out.println(this.title + "function, x = " + x);
    	}
	}
}

public class test {
	public static void main(String[] args) {
    	new MyThread("thread  A").run();
    	new MyThread("thread  B").run();
    	new MyThread("thread  C").run();
	}
}

The results are as follows:

thread  A function, x = 0
 thread  A function, x = 1
 thread  A function, x = 2
 thread  A function, x = 3
 thread  A function, x = 4
 thread  A function, x = 5
 thread  A function, x = 6
 thread  A function, x = 7
 thread  A function, x = 8
 thread  A function, x = 9
 thread  B function, x = 0
 thread  B function, x = 1
 thread  B function, x = 2
 thread  B function, x = 3
 thread  B function, x = 4
 thread  B function, x = 5
 thread  B function, x = 6
 thread  B function, x = 7
 thread  B function, x = 8
 thread  B function, x = 9
 thread  C function, x = 0
 thread  C function, x = 1
 thread  C function, x = 2
 thread  C function, x = 3
 thread  C function, x = 4
 thread  C function, x = 5
 thread  C function, x = 6
 thread  C function, x = 7
 thread  C function, x = 8
 thread  C function, x = 9

It can be seen from the results that he executes A first, then B, and then down in turn. This leads to A lot of waste of space resources in the middle, which is obviously similar to single thread.
For example, there is a start() method

class MyThread extends Thread { //Body class of thread
	private String title;

	public MyThread(String title) {
    	this.title = title;
	}

	@Override
	public void run() { //Thread theme method
    	for (int x = 0; x < 10; x++) {
        	System.out.println(this.title + "function, x = " + x);
    	}
	}
}

public class test {
	public static void main(String[] args) {
    	new MyThread("thread  A").start();
    	new MyThread("thread  B").start();
    	new MyThread("thread  C").start();
	}
}

After the execution here, it is found that it is alternating. This is multithreading, and through the call at this time, you can find that although the start() method is called, the final execution is the run() method, and all thread objects are executed alternately.

doubt? Why does the start() method in the Thread class have to be used instead of the run() method?
It's best to look at the start() method. Pay more attention to the relationship between the start() method and start0().

In any case, as long as multithreading is defined, there is always only one way to start multithreading: the start() method in the Thread class.

2. Realize multithreading with runable

Although the definition of multithreading can be realized through the inheritance of Thread class, there is always the limitation of single inheritance for inheritance in Java programs. Therefore, there is a second form of multithreading body definition structure in Java: realizing Java Lang. runnable interface, which is defined as follows:

@FunctionalInterface //Functional interface, JDK must be after version 1.8
public interface Runnable{
	public void run();
}

The implemented interfaces are as follows:

class MyThread implements Runnable { //The body class that implements the Runable thread
	private String title;

	public MyThread(String title) {
    	this.title = title;
	}

	@Override
	public void run() { //Main method of thread
    	for (int x = 0; x < 10; x++) {
        	System.out.println(this.title + "function, x = " + x);
    	}
	}
}

However, since the Thread parent class is not inherited at this time, the inherited method start() is no longer supported in the MyThread class at this time, but if Thread. Is not used The start() method cannot start multiple threads. At this time, you need to observe the construction method provided by the Thread class.
Here, the construction method of Thread class includes:

public Thread(Runnable target)Method of

You can find it yourself. ctrl + click the Thread class.

For example, start multithreading through Runable and Thread

class MyThread implements Runnable { //Body class of thread
	private String title;

	public MyThread(String title) {
    	this.title = title;
	}

	@Override
	public void run() { //Main method of thread
    	for (int x = 0; x < 10; x++) {
        	System.out.println(this.title + "function, x = " + x);
    	}
	}
}

public class test {
	public static void main(String[] args) {
    	Thread thread1 = new Thread(new MyThread("Thread object 1"));
    	Thread thread2 = new Thread(new MyThread("Thread object 2"));
    	Thread thread3 = new Thread(new MyThread("Thread object 3"));
    	thread1.start();
    	thread2.start();
    	thread3.start();
	}
}

Check the results after running. You can find that the results run alternately like the results of the previous single inheritance Thread. There is no limitation of single inheritance. There is no inheritance Thread. Instead, the Thread constructor is called to realize multithreading through Runnable. Such a design is a standard design.

Because the Runnable interface uses the definition of functional interface, it can also directly use Lambda expression to implement multithreading definition. (here, for the old fellow iron whose Lambda expression is not clear, we can refer to the Lambda expression of Java's column (note) column to learn about it. Note: Lambda expression must be JDK1.. After version 8!!!)

public class test {
	public static void main(String[] args) {
    	for(int x=0;x<3;x++){
        	final String title = "Thread object-"+x;
        	Runnable run = ()->{  //Lambda expression
          	for(int y=0;y<10;y++){
             	 System.out.println(title+"function. y="+y);
          	}
        	};
        	new Thread(run).start();
    	}
	}
}

It can be seen that the results are the same. In addition, it can be more concise, as follows:

public class test {
	public static void main(String[] args) {
    	for(int x=0;x<3;x++){
        	final String title = "Thread object-"+x;
        	new Thread(()->{
            	for(int y=0;y<10;y++){
                	System.out.println(title+"function. y="+y);
            	}
        	}).start();
    	}
	}
}

Note: if it's eclipse, the error red curve may be displayed. It doesn't matter. Close the project and open it again. You can also run it directly. There is no error. It may be a bug. It's not clear.
In the development, for the implementation of multithreading, the priority is to implement the Runnable interface, and the multithreading is always started through the Thread class object!

Topics: Java Multithreading