“Many people only know high concurrency, but do not know the relationship between high concurrency and multi-threading, and how to design technical solutions for real high concurrency.”

01 What is high concurrency

High Concurrency is a set of Concurrency problems that occur when a web system is set to respond to a large number of requests within a short period of time. For example, the Concurrency problem for 12306 is called Concurrency. Tmall Double 11 event). A large number of operations, such as resource requests and database operations, will be performed during this period.

02 High concurrency processing indicators

Some commonly used indicators of high concurrency are:

1. Response Time

Response time: Time for the system to respond to a request. For example, if it takes 200ms for the system to process an HTTP request, this 200ms is the system response time

2. Throughput

Throughput: The number of requests processed per unit of time.

3. QPS (Query Per Second)

QPS: number of response requests per second. In the Internet domain, the distinction between this metric and throughput is not so obvious.

4. Number of concurrent users

Number of concurrent users: specifies the number of concurrent users who use system functions. For example, in an instant messaging system, the number of simultaneous online users to some extent represents the number of concurrent users of the system.

The difference between high concurrency and multithreading

“High concurrency and multithreading” are often mentioned together, giving the impression that they are equal, but in fact, high concurrency does not equal multithreading

1. A multithreaded

Multithreading is a feature of Java, because now the CPU is multi-core multithreading, can execute several tasks at the same time, in order to improve the execution efficiency of JVM, Java provides this multithreading mechanism, to enhance the efficiency of data processing. Multithreading corresponds to the CPU, and high concurrency corresponds to access requests. You can use a single thread to process all access requests, or you can use multiple threads to process access requests at the same time.

In the old single-CPU era, a single task could only execute a single program at a point in time. Then came the multi-task stage, in which the computer could perform multiple tasks or processes in parallel at the same time. Although not in the true sense of the “same point in time”, but multiple tasks or processes share a CPU, and the operating system to complete the multi-task CPU switching, so that each task has a chance to get a certain time slice to run.

Later, it developed into multi-threading technology, which enables multiple threads to execute in parallel within a program. The execution of a thread can be thought of as a CPU executing the program. When a program runs in multiple threads, it is as if more than one CPU is executing the program at the same time.

In short, multithreading can be understood as: multithreading is a programming method to deal with high concurrency, that is, concurrency needs to be achieved by multithreading.

2. High concurrency

High concurrency is not something unique to JAVA, but rather a broad, language-independent concept for providing better Internet services.

Typical scenes, such as: 12306 snatching train tickets, Tmall Double eleven seconds killing activities and so on. A large number of operations, such as resource requests and database operations, will be performed during this period. If the high concurrency is not handled properly, not only the user experience is reduced (the request response time is too long), but also the system may break down, or even OOM exception and the system stops working.

If the system can adapt to the high concurrency state, it needs to optimize the system from all aspects, including hardware, network, system architecture, development language selection, data structure application, algorithm optimization, database optimization and so on… Multithreading is just one solution.

I personally invite all BATJ architecture masters to create a Java architect community group (group number: 673043639), committed to providing a free platform for Java architecture industry communication, through this platform to let everyone learn and grow from each other, improve their skills, make their level to a higher level, and successfully lead to the development of Java architecture technology masters or architects

The concurrent technology of multithreading

Java multithreaded programming will involve the following technical points:

1. Three elements of concurrent programming

An atomic atom is a particle that can no longer be divided. Atomicity in Java means that one or more operations either all execute successfully or all fail.

The order in which a program is executed is in the order in which the code is executed. (The processor may reorder the instructions)

Visibility When multiple threads access the same variable, if one thread makes changes to it, the other threads can immediately get the latest value.

  1. Five states of threads

Create state when a thread is created using the new operator

The ready thread calls the start method. The thread in the ready state does not necessarily execute the run method immediately and needs to wait for the CPU to schedule it

The running state CPU starts scheduling threads and executes the run method

Blocked thread execution is blocked for several reasons, such as calling sleep, trying to get a lock, etc

The run method was executed or an exception was encountered during execution

3. Pessimistic and optimistic locks

Pessimistic locking: Every operation locks, causing the thread to block.

Optimistic locking: An operation is completed without locking, assuming no conflicts. If the operation fails because of conflicts, retry until it succeeds without causing thread blocking.

4. Collaboration between threads: Wait, notify, notifyAll, etc

5. The synchronized keyword

6.CAS

CAS stands for Compare And Swap, And is a technique used to implement concurrent applications. The operation contains three operands — the memory location (V), the expected old value (A), and the new value (B). If the value of the memory location matches the expected original value, the processor automatically updates the location value to the new value. Otherwise, the processor does nothing.

7. The thread pool

Creating a thread while using threads is simple, but problematic. If there are a large number of concurrent threads, and each thread executes a short task and then terminates, creating threads frequently can greatly reduce the efficiency of the system because of the time it takes to create and destroy threads frequently. Thread pools can be reused to greatly reduce the performance cost of frequent thread creation and destruction.

High concurrency system solution

1. Distributed cache: Redis, memcached, etc., combined with CDN to solve the image file access.

2. Message queue middleware: activeMQ, etc., to solve the asynchronous processing ability of a large number of messages.

3. Application split: a project is split into multiple project deployments, and dubbo is used to solve the communication between multiple projects.

4. Database vertical split and horizontal split (sub-database sub-table), etc.

5. Database read and write separation to solve the query problem of big data.

6. You can also use NOSQL, such as mongoDB and mysql.

7. Service degradation and traffic limiting mechanism in the case of big data access need to be established.

The reason why someone is always better than you is because they are already better and are constantly trying to become better, while you are still satisfied with the status quo and secretly happy! Rational use of their every minute every second of time to learn to improve themselves, do not use “no time” to cover up their ideological laziness! While young, hard to fight, to the future of their own account!

Welcome to leave a message to discuss, pay attention to, continue to update!

To- Molin Java architecture To share the latest Internet articles pay attention To the latest development of the InternetCopy the code