Multi-threaded application of system operation and maintenance series

A thread is a path of program execution, and a process can contain multiple threads.

The difference between multithreaded parallelism and concurrency:
Parallelism means that two tasks run at the same time, requiring multi-core CPU resources; Concurrency means that both tasks request to run, but the processor can only accept the execution of one task, so the strategy adopted is two tasks Take turns, because the interval is short, we will feel that the two tasks are going on at the same time. That is, the CPU performance required for parallelism is higher.

The operating principle of Java programs:
Java commands start the Java virtual machine. Starting the JVM is equivalent to starting an application, that is, starting a process, which automatically starts a main thread, and then the main thread calls the main method of a certain class. Java at least starts the garbage collection thread and the main thread, so it is multi-threaded.

Multi-thread implementation method 1:
Define the class to inherit Thread;
rewrite the run method;
write the things done by the new thread in the run method;
create a thread object;
open a new thread, and the run() method will be automatically executed internally.
Code:

class MyThread extends Thread{
    
    
	public void run(){
    
    
		System.out.println("重写run方法");
	}
}

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

Multi-thread implementation method 2:
Define the class to implement the Runnable interface;
implement the run method;
write what the new thread does in the run method;
create a custom Runnable subclass object;
create a Thread object and pass in Runnable;
call the start() method to start For new threads, Runnable's run() method is automatically called internally.
Code:

class MyRunnable implements Runnable{
    
    
	public void run(){
    
    
		System.out.println("重写run方法");
	}
}

public static void main(String[] args){
    
    
	MyRunnable mr = new MyRunnable();
	Thread t = new Thread(mr);
	t.start();
}

The difference between the two methods:
inheriting Thread, because the subclass overrides the run method of the Thread class, when calling start, directly find the run method of the subclass;
when implementing Runnable, pass in the reference of Runnable in the constructor, and the member variables are remembered It, when start() calls the run method, internally judges whether the reference to the member variable Runnable is empty. When it is not empty, compile to see the parent class run, and run to see the subclass run; the
former: you can directly use the methods in the Thread class, the application is simple, but If you already have a parent class, you cannot use this method; the latter: the thread class defined by yourself does not matter even if there is a parent class, the interface can be implemented more, but it cannot directly use the methods in the Thread class, you need to get it first Thread objects can be used, and the code is more complicated.

PS: Digression: By comparing the two implementations, it can be found that the first is simple but has limitations in use; the second is complex but can be implemented more. Nothing is perfect. We want to be open to the world, seek common ground while reserving differences, be strict with ourselves, and be lenient with others~~~ It's a long way to go. . .

Guess you like

Origin blog.csdn.net/langxiaolin/article/details/113834251