The directory structure

  • Understand concurrency and the value of concurrency
  • Understand the thread
  • Implementation of threads in Java
  • Principle of multithreading level
  • Start and stop of threads

[High concurrency] The number of concurrent requests that the system can handle at the same time (the web page can be viewed by 100,000 users at the same time, and the system can handle 100,000 requests at the same time) TPS: the number of transactions processed per second increases, deletes, and changes QPS: 1000 users initiate queries at the same time, and 1000 users obtain results at the same time, indicating that the QPS reaches 1000

How to support high concurrency?

Hardware resources

  1. CPU: Number of cores: Determines the number of tasks that can be parallel
  2. Memory: Memory stores data and improves performance
  3. Disk: SSD, mechanical hard disk
  4. Network card: GIGABit, 10 gigabit

.

Software resources

  1. CPU: Threads are the smallest unit of CPU execution, with 8 cores that can run up to 8 threads at a time

  2. IO: Interacts with the database and saves to the database. -> Flush to disk. I/O performance affects the query speed. Data database and table (reduce the amount of data to improve the performance of computing), distributed cache (reduce the impact of IO performance, non-organizational data), distributed message middleware (asynchronous, first tell the user the results have been completed, the program processing in the back end)

  3. The single node encountered the bottleneck, so the order was placed on A server for calculation, and the inventory was placed on B server for calculation. The software and hardware optimization agent revenue: QPS1000 -> QPS10000

Multithreading technology

What is a thread

  1. Java program ->.java source file (disk) -> JVM (compiled). Class ->main method to run this program (loaded into memory) -> generate a process
  2. The CPU executes the instructions in the process
  3. Threads are the smallest unit that the operating system can schedule, and the smallest unit that the CPU can schedule.

Suppose there is an operation to load a file from disk and save it to the database. CPU computing efficiency <-> DISK I/O efficiency. If the I/O efficiency is low, the CPU is blocked and CPU resources are wasted. Consider: when a process is blocked due to IO, can the CPU switch to another process to execute it? Multi-channel programming: multi-process, so that multiple processes are loaded into memory at the same time, processes are directly isolated from each other. Time – sharing system: CPU line switching

Threads -> Lightweight processes N threads can exist in a process

  • Load disk -> Allocate thread execution
  • Save to database
  • .

Single thread multithread

Characteristics of threads

  • Asynchronous (register -> Send email)
  • Parallel (number of CPU cores)

Ideas go back and forth like this: read a big file, read it together. Sharding idea: read 1000 and assign one thread to process. For example, it takes one person a month to develop a system. It takes 30 people to develop and one day.

How are threads used in Java?

Using threads in Java is particularly simple

  • Inheriting Threads (also implementing Runnable)
  • Implement the Runnable interface
  • Callable/Future
  • CompletableFuture optimization based on Callable/Future
  • Dubbo, Nacos and other Ali source code commonly used CompletableFuture

New Thread(new SmsSenderTask()).start(). The start() method is called by the JVM, preferably by calling run().

– Thread pool is recommended to control the number of threads and reuse of threads.

  • Java starts the final execution of a thread

  • What’s the use of threads? Where can I use it? Wherever you need to do parallel processing and asynchronous processing, you can think about it for a moment. You just didn’t think about it.

Principle of threads

The declaration cycle of a thread

Threads start -> end Start () starts a thread. When execution in the thread is finished, it is automatically destroyed, and run() terminates any other state of the thread

  • Thread.sleep(), join(), wait(), WAITING, TIME_WATING
  • Locksupport. park(this) blocks the thread from preempting the lock

Thread to stop

Stop Stops the thread kill -9 Normally

Interrupt () stops the thread

  • Threads can be automatically terminated when controllable, without interruption.
  • Wait () and sleep() terminate only when run() is out of control and cannot be terminated automatically.
  • With thread pools you don’t have to interrupt by yourself, thread pools are already wrapped
  • Interrupt is a native method