Understanding of threads (1)

Thread

The relationship between CPU-process-thread
We know that the process is the smallest computing unit
of the CPU, and the thread is the smallest execution unit of the CPU.
In this process, the execution right of the thread can be understood as who the CPU opens the valve of the information flow.
The thread is the path of the marked information flow in the computer. When the CPU opens the valve to whomever, the marked information flow is conveyed.
The purpose of the thread is also to transport the information flow. Improve the transmission efficiency of information flow.
The essence of threads—allowing codes to be executed out of order—different codes can be executed separately during the process of CPU calculation—threads mark the code that needs to be executed. Threads have their own numbers, and different numbers have different functions.

Single thread

单线程 代码的执行顺序不变 多线程 代码的执行顺序取决于执行CPU的线程是哪个。

Multithreading

同一个进程中开启了多条执行的路径即线程,线程之间相互不影响,且同时执行。即多段代码同时执行。
将内存开辟为不同的区域,按区域的执行,代码在不同的内存区域中。
标记线程执行的进度,和线程的编号。

Advantages of multithreading

1. Improve the efficiency of the program
(1) Parallel at the same time (2) Asynchronously send data (3) The back-end time-consuming code can be implemented asynchronously
2. The back-end uses multi-threaded background
HTTP uses a synchronous form by default, the whole The process is based on response. If the server does not respond to the client in a timely manner, the client will wait all the time and the user experience will be bad.
3. Each thread does not affect each other.

Multi-threaded CPU switching process-CPU and core

对于如果使用单核CPU服务器的时候,开启多线程的情况下,并不是真正意义上的多线程,因为单核CPU服务器同一个时刻只能执行一个线程,单核CPU的服务器的执行真正意义上是并发的执行,即不同线程的快速切换。
对于多核CPU的服务器在开启多线程的时候,有多少个核就能够执行多少个线程,实现并行的执行线程的任务。

Is multithreading the more the better

CPU的切换会影响服务器的性能。
如果项目小可以采用多线程实现异步,如果项目大建议通过mq实现异步。
如果是一个高并发项目建议采用MQ替代多进程。

How to create multiple threads

1.继承Thread 重写run方法
2实现Runnable 接口 参数回调 装饰者设计模式
3.带返回结果的线程 Callable---Futuretask 底层开启一个线程 但执行顺序还是单线程的
4.线程池---四种实现方式
5.Spring异步直接使用	@Async注解 通过代理模式创建。
public class ThreadPool {
    
    
 public static void main(String[] args) {
    
    
  {
    
    
   ExecutorService exe = Executors.newCachedThreadPool();//多线程的创建方法
   System.out.println( Thread.currentThread().getName());
   exe.execute(()->System.out.println(Thread.currentThread().getName()));
  }
 }
}

User thread and daemon thread

用户线程不会在主线程停止后停止。   t1.setDaemon(false);
守护线程会在主线程停止后停止。 t1.setDaemon(true);

How to stop a thread gracefully

public class EndThread extends Thread{
    
    
  static boolean flag = true;
 public void run() {
    
    
  while(flag) {
    
    
 //线程的内容
  }
 }
 public static void main(String[] args) {
    
    
  EndThread endThread = new EndThread();
  endThread.start();
  flag=false;
 }
}

The difference between wait and sleep

wait------
sleep----
都是可以让当前线程阻塞。wait可以释放锁,
为什么wait可以放在object类中
	因为synchronizd可以使用任意对象为锁。

join method

哪个线程调用该方法 哪个线程阻塞
	调用join方法时 主线程会进入线程池进行休眠 其他的线程依旧在执行。也就是使用该方法的线程会优先执行。

Guess you like

Origin blog.csdn.net/qq_42351519/article/details/112124765