[Multi-thread performance tuning] Multi-thread tuning (Part 1): Which operations cause context switching?

  In a concurrent program, starting more threads does not allow the program to execute concurrently to the maximum extent . If the number of threads is set too small, the program will not be able to fully utilize system resources; if the number of threads is set too large, it may cause excessive competition for resources, resulting in additional system overhead caused by context switching.

What is a context switch

  The time slice determines how long a thread can run continuously on the processor . When the time slice of a thread runs out, or is forced to suspend running due to its own reasons, at this time, another thread (which can be the same thread or a thread of another process) will be selected by the operating system to occupy the processor. This process in which one thread is suspended and deprived of the right to use, and another thread is selected to start or continue to run is called context switching (Context Switch).
  What does the context include? Including the storage content of the register and the instruction content stored by the program counter.

Incentives for multithreaded context switching

  In multi-threaded programming, we mainly face performance problems caused by context switching between threads.

insert image description here
  Spontaneous context switching means that a thread is switched out by a Java program call. In multi-threaded programming, calling the following methods or keywords often triggers spontaneous context switching.

  • sleep()
  • wait()
  • yield()
  • join()
  • park()
  • synchronized
  • lock

  Non-spontaneous context switching means that the thread is forced to switch out due to the scheduler. The common ones are: the allocated time slice of the thread is exhausted, the virtual machine garbage collection is caused, or the execution priority is caused.
  Why does virtual machine garbage collection cause context switching? The use of the virtual machine garbage collection mechanism may lead to stop-the-world events, which is actually a thread suspension behavior .

Discover context switching

  Concatenated execution is faster than concurrent execution because of the additional overhead caused by thread context switching. The specific links in the switching process where the system overhead occurs are summarized as follows:

  • The operating system saves and restores the context;
  • The scheduler performs thread scheduling;
  • Processor cache reload;
  • A context switch may also cause the entire cache area to be flushed, which incurs a time overhead.

When to use multithreading?

  Generally, when a single logic is relatively simple and the speed is relatively very fast, we can use a single thread. For example, Redis reads values ​​quickly from memory without considering the blocking problem caused by I/O bottlenecks. In scenarios where the logic is relatively complex, the waiting time is relatively long, or a large amount of calculation is required, it is recommended to use multithreading to improve the overall performance of the system. For example, NIO's file read and write operations, image processing, and big data analysis.
  When using Synchronized in multithreading, context switching between processes will also occur. Where will it happen? Process context switching refers to switching back and forth between user mode and kernel mode. If once the Synchronized lock resource competition is fierce, the thread will be blocked, and the blocked thread will call the kernel state from the user state to try to obtain the mutex. This process is a process context switch.

Guess you like

Origin blog.csdn.net/ChinaLiaoTian/article/details/123171873