Interviewer: It says on your resume that you’re blogging about concurrent programming, right?

Angela: I’ve been doing a lot of reading and writing. Thanks to the readers, I’m going to do that.

Interviewer: Don’t talk to me like this, “nothing to do”? Doesn’t Ali use 996?

Angela: Did you know that? . Is the daily life of a tech person 996?

Interviewer: Come on, let’s get to the point. Why don’t you tell me what concurrency is?

Angela: Concurrency is the existence of two or more threads that manipulate resources on the same physical machine at the same time.

Interviewer: What’s the difference between concurrency and parallelism?

Angela: Here’s an example from life:

  • You are playing king of Glory, this time girlfriend find you video, you have been playing king of Glory to pick up, that you do not support concurrent (also do not support parallel);

  • You are playing king of Glory, this time girlfriend sent you wechat, you quit king of Glory, back to the wechat back to the king, wechat and king switch back and forth, that you support concurrent, but do not support parallel;

  • You are playing king of glory, this time girlfriend to call you, you play glory while answering the phone, that you support parallel.

Parallel key point is the physical “” at the same time, we are in a single core CPU, both to write code also listening to music, the multithreading is based on the actual operation system based on CPU time slice to do the task of rotation, is false” at the same time, “can only be said to be concurrent, not parallel, but multicore cpus can support each nuclear run tasks at the same time, is the real” “at the same time, is parallel.

Joe Armstrong, the father of Erlang, drew a diagram explaining the difference between Concurrent and Parallel.

Concurrency allows two groups of children to use the coffee machine in turn. In parallel, there are two coffee machines at the same time, and there is no conflict between two groups of children.

Interviewer: What about high concurrency? Do you know about high concurrency?

Angela: [thinking, it’s time to build a rocket]

You say “High Concurrency,” right?

We usually talk about concurrent, more focus is thread-safe, but discussing high concurrency, focus is not only a thread safety problem, but how to handle a large number of requests in a short time, ensure that the system response time and throughput, reliable and more focused on the stability problem (SRE), high concurrency involves the complete system of knowledge, Thread safety is only a small part of it.

High concurrency is one of the important factors to be considered in the current Internet design system. Generally speaking, strict design is adopted to ensure that the system can handle many requests in parallel at the same time. This is often referred to as “high concurrency”. This means that the system can serve many requests at a time without affecting system performance. If you want to design a high availability and high performance system, we should consider from many aspects, such as hardware, software, programming language choice, network consideration, the overall architecture of the system, data structure, algorithm optimization, database optimization and so on. There’s a lot to be said for each of these points, and Angela will update this later in the course.

Interviewer: Can you tell me the QPS of your system?

Angela: You can have a QPS of 10W+ in the big push scenario, 2W+ in the peak hours of daily business, and several thousand at other times.

In fact, for most of the system, dozens, hundreds of very normal, QPS can be over a thousand is not low, some business will have a peak, QPS stable over ten thousand system is not much in reality, so we can pay attention to their system QPS, this question is often asked in the interview.

Interviewer: What tools do we have to simulate concurrent requests?

Angela: PostMan, Apache Bench (AB), Jmeter, Jmeter is recommended.

Interviewer: Can you write some code to demonstrate concurrency security?

Angela: Sure. Pass me the pen and grab the A4 paper for me.

public class ConcurrencySafeTest { private static int counter = 0; Public static void main(String[] args) {// Use ThreadPoolExecutor threadPool = (ThreadPoolExecutor) Executors.newCachedThreadPool(); For (int I = 0; i < 2000; i++) { threadPool.submit(new Add()); } threadPool.shutdown(); System.out.println(counter); } static class Add implements Runnable { @Override public void run() { counter++; }}}Copy the code

We count 2000 times. The expected result should be 2000, but the actual result is as follows:

1971, 1987,Copy the code

Because “Concurrency” series starts from the basics, the above part of the code is related to some of the following content, such as thread pool and the use of threads, here just have a general understanding of concurrency safety issues, will be explained in detail later, the questions of the interviewer as an extension to read.

Interviewer: See you used CachedThreadPool in your code. How many threads were created in the CachedThreadPool for 2000 tasks?

Angela: Not really. CachedThreadPool caches threads (multiplexed threads) and does not queue tasks. When you take a task, you either reuse an existing thread or create a new one. So how do we determine how many threads have been created in the thread pool? You can add a code and print it out.

As follows:

private static int counter = 0; Public static void main(String[] args) {// Use ThreadPoolExecutor threadPool = (ThreadPoolExecutor) Executors.newCachedThreadPool(); For (int I = 0; i < 2000; i++) { threadPool.submit(new Add()); } threadPool.shutdown(); / / print maximum number System. Using a thread out. The println (" largestPoolSize: "+ threadPool. GetLargestPoolSize ()); System.out.println(counter); }Copy the code

The following output is displayed:

LargestPoolSize :11 1937 largestPoolSize:14 1956 largestPoolSize:31 1970Copy the code

As you can see, it’s different every time. Thread pools have been covered in previous articles and will be covered in more detail later in this series.

For largestPoolSize, the comment states that the maximum number of threads in the thread pool is recorded.

/**
* Tracks largest attained pool size. Accessed only under
* mainLock.
*/
private int largestPoolSize;

Copy the code

Interviewer: What’s the difference between shutdown and shutdownNow?

Angela: Shutdown sets the state of the thread pool to SHUTWDOWN. Tasks that are being executed continue, and those that are not are interrupted. ShutdownNow, on the other hand, sets the state of the thread pool to STOP. Tasks that are being executed are stopped and those that are not are returned.

Source code comparison:

//shutdown public void shutdown() { final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { checkShutdownAccess(); // Set the thread pool state to SHUTDOWN advanceRunState(SHUTDOWN); interruptIdleWorkers(); onShutdown(); // hook for ScheduledThreadPoolExecutor } finally { mainLock.unlock(); } tryTerminate(); }Copy the code
public List<Runnable> shutdownNow() { List<Runnable> tasks; final ReentrantLock mainLock = this.mainLock; mainLock.lock(); try { checkShutdownAccess(); // Set the thread pool state to STOP advanceRunState(STOP); interruptWorkers(); Tasks = drainQueue(); } finally { mainLock.unlock(); } tryTerminate(); return tasks; }Copy the code

Interviewer: What are the states of a thread pool?

Angela: five. Note that we are talking about the state of the thread pool, not the state of the thread. Here is the thread pool state flow diagram:

This article is the first in the series of “Concurrency”, mainly introduces some of the basic concepts of concurrency, parallelism, high concurrency, and concurrent security issues, and the next episode will cover the risks and advantages of concurrency and CPU multi-level cache, as well as some instructions for memory operations, and then discuss the Java memory model.

Full outline reference: