Before we learn about multithreading, let's take a look at processes
Process: the program currently running, which can be understood as the execution area of an application in memory. For example, when we open the task manager in the computer, we can see that there are many processes in the background.
Thread: a thread is actually an execution control unit in a process, that is, its execution path
There can be one or more threads in a process.
Multithreading: high efficiency, but low security
Single thread: low efficiency but high security
In JAVA, we learn that multithreading mainly involves Thread class.
There are two ways to create a new execution thread.
(1) declare the class as a subclass of Thread. This subclass should override the run method of the Thread class. Next, you can assign and start an instance of the subclass. Let's take a look at an example:
public class Mythread extends Thread { //Mythread is a subclass of Thread, overriding the run method @Override public void run() { // TODO Auto-generated method stub for(int i=0;i<50;i++) { System.out.println(i); } } } public class ThreadDemo { public static void main(String[] args) { MyThread mt=new MyThread(); //Create thread instance mt.start(); //Start the thread. Note that we start with the start method } } //The result of running is to output the number between 0-49.
The above knowledge creates a single thread example, and we can't see how multiple threads execute. So we create multiple threads and give them names.
public class MyThread extends Thread { @Override public void run() { // TODO Auto-generated method stub for(int i=0;i<5;i++) { System.out.println(getName()+": "+i); //Get the thread name by calling the getName() method } } } public class ThreadDemo { public static void main(String[] args) { //Create thread instance MyThread mt=new MyThread(); //Startup thread mt.start(); MyThread mt2=new MyThread(); mt2.start(); } } /* The output is Thread-0: 0 Thread-1: 0 Thread-0: 1 Thread-0: 2 Thread-1: 1 Thread-0: 3 Thread-1: 2 Thread-0: 4 Thread-1: 3 Thread-1: 4 */
I suddenly think this name is not good-looking. I want to set the name for the thread myself. What should I do? At this time, we tried the method of void setName(String name)
public class ThreadDemo { public static void main(String[] args) { //Create thread instance MyThread mt=new MyThread(); //Modify thread name mt.setName("Mould mold"); //Startup thread mt.start(); MyThread mt2=new MyThread(); //Create thread instance mt2.setName("Silly face"); //Modify thread name mt2.start(); //Startup thread } } /* Mildew: 0 Mildew: 1 Silly face: 0 Mildew: 2 Silly face: 1 Mildew: 3 Silly face: 2 Mildew: 4 Silly face: 3 Silly face: 4 */
Here is a point to note that the main method we usually use is single thread!
(2) another way to create a Thread is to declare a class that implements the Runnable interface. The class then implements the run method. You can then assign an instance of the class to pass as a parameter and start when you create a Thread.
public class MyThread implements Runnable { @Override public void run() { for(int i=0;i<5;i++) { Thread t=Thread.currentThread(); System.out.println(t.getName()+": "+i); //We can also use chain programming to achieve System.out.println(Thread.currentThread().getName()+": "+i); } } } public class ThreadDemo { public static void main(String[] args) { //Create thread instance MyThread mt=new MyThread(); //First, complete the implementation object of a Runnable class Thread t=new Thread(mt); //Modify thread name t.setName("Telles"); //Startup thread t.start(); //Create thread instance MyThread mt2=new MyThread(); Thread t2=new Thread(mt); t2.setName("Silly face"); //Modify thread name t2.start(); //Startup thread } } /*Telles: 0 Silly face: 0 Telles: 1 Telles: 2 Silly face: 1 Telles: 3 Silly face: 2 Telles: 4 Silly face: 3 Silly face: 4 */
Here we have a way
static Thread currentThread() /*Used to return the current Thread object. In the above example, we can't directly use getName(), so the Thread object created first is used to call getNmae().*/
After reading the above two methods of creating threads, we may have a question: why do we need to integrate a Runnable interface since we have inherited threads? Isn't that a superfluous act? Let's recall what we learned earlier. In JAVA, there is only single inheritance. If we use the inherit Thread method, it is impossible for us to inherit other classes later. Therefore, we recommend the second method to create threads.