Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the core of learning concurrent programming

2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail what is the core of learning concurrent programming. The quality of the article is high. Therefore, Xiaobian shares it with you as a reference. I hope that after reading this article, you will have a certain understanding of relevant knowledge.

From a distance, concurrent programming can be abstracted into three core issues: division of labor, synchronization/collaboration, and mutual exclusion.

If you already work, then you must have heard of or are applying agile development patterns to deliver day-to-day work tasks. Let's explain these three core problems using familiar processes

division of labor

Split the current Sprint Story into appropriately sized Tasks and assign them to appropriately sized Team Members to complete.

There are two "appropriate" here, and it is very important to split the Story into medium-sized, completable Tasks. The granularity of splitting is too coarse, which makes it difficult to complete this task, takes a long time, and is difficult to cooperate with others; the granularity of splitting is too fine, which leads to too many tasks, which are difficult to manage and track, and wastes energy and resources. (The right thread can better complete the whole work, of course, one thread can easily do it, there is no need for multi-threading); arrange for the right person to complete the same important, UX-UE problem to the back-end personnel to handle, it is obviously problematic (the main thread should do things to the child thread obviously can not solve the problem, each thread can do the right thing to play a role)

About division of labor, common Executor, producer-consumer model, Fork/Join, etc., these are all embodiments of division of labor thought.

Synchronization/Collaboration

After the task splitting was completed, I had to wait for Zhang San's task, and Zhang San had to wait for Li Si's task. In other words, there was a dependency relationship between the tasks. After the previous tasks were completed, the subsequent tasks could be executed. The high-level person could confirm repeatedly through communication to ensure that his own tasks could be executed. But when faced with programs, we need to understand how programs communicate, how one thread executes a task and notifies subsequent threads to execute

All synchronization/collaboration relationships can be represented by the familiar If-then-else:

if(Preorder task completed){ execute();}else{ wait();}

The above code means: when a certain condition is not met, the thread needs to wait; when a certain condition is met, the thread needs to be awakened for execution. The cooperation between threads may be the cooperation between the main thread and the child thread, or the cooperation between the child thread and the child thread. CountDownLatch and CyclicBarrier in Java SDK are used to solve the thread cooperation problem.

mutex

Division of labor and synchronization emphasize performance, but mutual exclusion emphasizes correctness, which is what we often refer to as "thread safety." When multiple threads access a shared variable/member variable at the same time, uncertainty may occur. The uncertainty is mainly caused by visibility, atomicity, and orderliness. The core of solving these problems is mutual exclusion.

mutex

Only one thread is allowed to access shared variables at a time

Look at the picture below. The trunk road is a shared variable. There can only be one car at a time when you enter the trunk road. Do you understand this? "Great things in the world will be divided for a long time."

Java SDK also has many mutually exclusive solutions, such as synchronized keyword, Lock, ThreadLocal, etc.

summary

Capitalists frantically exploit the surplus value of the working people to obtain the maximum return. When you face CPU, memory, IO these labor workers, you are the capitalist, you have to think about how to fully extract their value

When a worker can do the work, never let two people do it (single thread can satisfy, there is no need for multi-thread) When multiple workers work, let them have a clear division of labor, smooth cooperation, and no contradiction.

When the task is very large, because IO works slowly and CPU works fast, there is no need to let CPU die and wait for the current IO, and then execute other instructions. This is to extract residual value. How to extract its value to the maximum extent involves subsequent tuning problems, such as how many threads are suitable.

Division of labor is design, synchronization and mutual exclusion are realization, there is no good design, so in the division of labor stage, it is strongly recommended that you draw sketches and understand the bottleneck, so that there will be better implementation. In the following chapters, I will also lead you to draw sketches and analyze problems, and gradually develop this habit.

The contents of this chapter can be summarized simply with the following diagram. We will gradually light up the contents of leaf nodes. At this stage, don't pay too much attention (if you come up and gnaw on JDK source code, you may be painfully lost and eventually give up your advanced path).

To understand the three core problems, you should fully combine the reality of life, concurrent problems in the program, basically can find prototypes in real life.

The content of the next article, we will talk about, causing thread safety three problems: "visibility, atomicity, order", which involves a little content of JMM, you can understand in advance, so that we can better collision

soul inquiry

Are there many scenarios for multithreaded programming at work?

When you think of multithreading, do you think only of synchronized?

Java packages each class, do you understand the underlying implementation and design philosophy?

Efficiency tools

What are the core of learning concurrent programming to share here, I hope the above content can be of some help to everyone, you can learn more knowledge. If you think the article is good, you can share it so that more people can see it.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report