If you want to learn concurrent programming, a thorough understanding of these three core is the key

From a distance, concurrency, concurrent programming can be abstracted into three core issues: the division of labor, sync / collaboration, mutual exclusion

If you have been working, then you must have heard or are applying agile development model to deliver daily tasks, we use to explain the process you are familiar with the three core issues

Division of work

The current split into Sprint's Story "appropriate" size of the Task, and arrange to "appropriate" to complete the Team Member

There used two "proper" Story split into moderate size, to be completed by the Task is very important. Split too coarse, leading to the difficulty of this task is completed is high, time-consuming and difficult to work with others; split size is too small, has led to too many tasks, poor management and tracking, waste of energy and resources. ( Suitable thread in order to better complete piece of work, of course, a thread can easily get no need multi-threading ); arrangements to the appropriate personnel to accomplish equally important, UX-UE issues to the back-end processing staff, apparently there is a problem ( the main thread is supposed to do to the child thread is obviously not solve the problem, each thread is doing the right thing can play a role )

Division of Labor, the common Executor, producer - consumer model, Fork / Join, etc., which are reflected in the division of thought

Sync / Collaboration

Task split is completed, I have to wait for the seating of the tasks, such as John Doe Joe Smith to task, that is to say there is a dependency between tasks, completed tasks to perform in front of, behind the task can only be performed by people in senior communication repeatedly confirmed, to ensure that their tasks can be started. But the face of the program, we need to understand the communication process, a thread executing the task, how to notify subsequent thread execution

All sync / collaboration we can use you are most familiar with If-then-else to represent:

if(前序任务完成){ execute(); }else{ wait(); } 

The above code is to say: when a certain condition is not met, the thread needs to wait; when a certain condition is met, the thread needs to be awakened to perform , the collaboration between the threads may be in collaboration with the main thread child thread, it may be sub-thread cooperation with sub-threads, Java SDK in CountDownLatch and CyclicBarrier thread is used to solve the problem of collaboration

Exclusive

Division of labor and synchronization emphasized that performance, but exclusive emphasis on correctness , is what we often referred to "thread-safe" when multiple threads simultaneously access a shared variable / member variables, uncertainty can occur, resulting in no the main uncertainty is visible, atomicity, ordering these three issues, the core of solving these problems is mutually exclusive

Exclusive

The same time, allowing only one thread to access shared variables

Look at the chart, the trunk road is shared variables into the main road can only have a car, so if you understand it? " World affairs, long period of division ."

 

The same Java SDK, there are many mutually exclusive solutions, for example, you'll be able to think of the synchronized keyword, Lock, ThreadLocal so is mutually exclusive solutions

to sum up

Crazy capitalists extract surplus value of labor workers, to get maximum benefit. When you are faced with the CPU, memory, IO these labor workers, you are the capitalists, you need to think about how to best extract their value

When a worker capable of living, never let two people to dry (not necessary in order to meet the single-threaded multi-threaded)

When multiple workers to work, we should let them clear division of labor, cooperation and smooth, no contradiction

When the task is large, due to the slow work IO, CPU work fast, there is no need to let the current IO CPU die, etc., turn to execute other instructions, this is the surplus value, how to maximize the extraction of its value, which it comes to subsequent tuning issues, such as how many threads and other appropriate

The division is to design, synchronization and mutual exclusion is achieved , there is no good without a good design to achieve, so in the division stage, it is strongly recommended that you outline sketch, to understand where the bottlenecks are, as this will better achieve subsequent chapters content, I will lead the sketches, analyze problems, and gradually develop the habit

In this chapter you can simply use the following chart summarizes the contents of leaf nodes we will gradually light up, at this stage without too much attention (if you chew up on the JDK source, you might lose the pain, and eventually give up your Advanced the road)

 

Understanding the three core issues, you have to be fully integrated real life, concurrency problems in the program, basically can find the prototype in real life

Published 61 original articles · won praise 17 · views 1397

Guess you like

Origin blog.csdn.net/cxytony/article/details/103734808