Take you to understand the truth of multithreading (Corgi version)

1. Problem background

Recently, a friend who has been working for several years sent a private message saying that he wants to change jobs recently, but he has almost forgotten the basic content of Java, especially multi-threading, so he wants me to sort out multi-threading for him. Content. So, let's arrange it for you today.

what is a process

Before reviewing threads, let me tell you about the concept of process.

The so-called process is an independent unit for the system to allocate and call resources, and each process has its own independent memory space and system resources.

Current computer systems can be divided into single-process and multi-process systems, the differences are as follows:

  • Single-process operating system: dos (only one task can be executed at a time)
  • Multi-process single-user operating system: Windows (can only perform multiple tasks at a time)
  • Multi-process multi-user operating system: Linux (only multiple tasks can be performed at a time)

what is thread

Then let's take a look at what a thread is. The so-called thread is an execution path in the process, and multiple threads can share the memory space and system resources in the process. There can be multiple threads in a process, and each thread has a different division of labor.

So what is the relationship between processes and threads? Take a look at the summary for everyone:

  • Between processes and processes, their memory space and system resources are independent;
  • Between multiple threads in the same process, the memory space and system resources are shared;
  • There can be one or more threads in a process;
  • If there is only one thread in a process, this thread is called the main thread;
  • Threads are in the process, and they are in the relationship of containment and containment.

How to create threads

So how do we create a thread? In general, we can use the following methods.

1. Inherit the thread class

In this way, we can create a MyThread class to inherit Thread, and rewrite the run method, the code is as follows:

public class Test01 {
    
        
    public static void main(String[] args) {
    
      
        //创建线程的对象  
        MyThread t = new MyThread();  
        //启动线程  
        t.start();  
    }    
}  
//线程类  
class MyThread extends Thread{
    
       
    //当前线程抢到cpu资源后,就会执行run方法  
    @Override  
    public void run() {
    
      
        System.out.println("当前线程抢到资源了");  
    }  
}

2. Implement the Runnable interface

The second way is to implement the Runnable interface. We can create a Task class, implement the Runnable interface and override the run method.
insert image description here

In addition, we can also create threads by implementing the Callable interface or using the Executors thread pool. These two methods will not demonstrate the implementation code. Interested students can private message to get the tutorial.

Summarize

Finally, let me summarize today's key points for you:

  • Process-to-process relationship: Processes exclusively share memory space and system resources;
  • The relationship between threads and processes: a process contains at least one thread;
  • The relationship between threads: In the same process, multiple threads share memory space and system resources;
  • A process can contain multiple threads, but there is only one main thread.

If you have any questions, you can leave a message, and I will help you solve it as soon as I see it!

Guess you like

Origin blog.csdn.net/GUDUzhongliang/article/details/130485575