Java Multithreading and High Concurrency Basics (1) - Basic Concepts You Must Know

For multi-threading and high concurrency, we must know the basic concepts involved here, just like learning martial arts, when learning a complete set of moves, you must learn to hold steady.

We first introduce several concepts that must be known in multi-threaded high concurrency.

1. Synchronous and Asynchronous

Synchronous and asynchronous are usually used to describe a request call. We often involve in front-end development, such as synchronous and asynchronous requests of ajax. So what are synchronous and asynchronous?

Synchronization means that once a request call starts, it must wait for the request to be processed before subsequent actions can be performed. For example, Xiao Ming has a habit of eating, sleeping, and playing beans every day. Must get a good night's sleep.

So what is asynchrony? Asynchrony means that after the request is initiated, we do not need to wait for it to complete, and the real request will be executed by another thread. If the asynchronous request needs to return a result, it will notify when the asynchronous request call is completed. The caller's true result.

There is a very classic example, the story of Lao Wang boiling water. (I suddenly remembered our colleague, the old driver)

The old driver is an employee who loves to learn. One day, the old man wants to boil water. He fills the water and puts it on the stove. Because he does not know when the water will boil, the old driver must keep staring at the water in the kettle (old driver, you Is it stupid?), and then go to learn after the water is boiled. This is an example of synchronization, watering --> waiting for the water to boil --> learning.

Suddenly one day, the old driver found that he was too stupid, so the old driver bought a sounding kettle. After the water boiled, he would make a "drip" sound by himself. So the process became like this, the old driver filled the water, Put it on the stove, and then go to learn. When the water boils, the kettle will beep, and the old driver will know that the water is boiling. This is an example of asynchrony.

2. Concurrency and Parallelism

Concurrency and parallelism are often asked in interviews.

If you have been exposed to the course of operating systems, we should know that in the computer, multiple tasks are performed at the same time, and they are executed in time slices in the operating system, but this detail is transparent to the outside world. Therefore, our computer can look like Execute multiple tasks at the same time.

Strictly speaking, parallelism is the real execution of multiple tasks at the same time, which benefits from the development of our technology, because our computers are basically multi-core (multiple processors). Therefore, it is possible to achieve parallel execution of tasks in a true sense. .

If there is only one core (single CPU) in the system, the tasks are executed concurrently . Because there is only one processor, everyone can only execute their own tasks according to the allocated time slice , so we feel that the previous computers are really stuck and don’t need it. .

3. Critical section

In multithreading, a critical area refers to a common resource or shared data that multiple threads need to access at the same time . But this area can only be used by one thread at a time. Once a thread acquires it, other threads must wait.

4. Blocking and non-blocking

Blocking and non-blocking are usually used to describe the interaction between multiple threads . For example, if one thread occupies a critical section, then other threads have to wait, that is to say, other threads are blocked, which is the case of blocking. Non-blocking is the opposite of blocking, that is, multiple threads can enter at the same time, you don't block me, I don't block you.

5. Deadlock, starvation, livelock

Deadlock, starvation, and livelock are the liveness problems of multithreading .

Deadlock ( Deadlock ) is the worst case, because no thread can continue to execute. For example, four cars form a ring on top of their heads. Now you understand the use of traffic lights.

Starvation (Stavation) means that one or more threads have not been able to obtain the required execution resources (CPU resources) for some reason, resulting in no execution, and starvation will occur in the case of unfair locks. For example, When we were eating, you ordered food from the waiter, and everyone who came before you ate it. If you haven't eaten yet, who else will be hungry if you're not hungry. Haha, that's why there was a calling machine ( Please pick up the meal with number xx, what a familiar voice).

 

Livelock means that a thread adheres to the principle of modesty and comity. As long as other threads want to use resources, it will take the initiative to release resources to others for use. If both threads are polite and both actively release resources, it will form Livelock situation (is this thread stupid?). For example, when you are walking, you run into someone head-on, they both give way to each other, and then never go on. [covering face]


 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326004423&siteId=291194637