Categories
Uncategorized

Learn concurrent programming, a thorough understanding of these three core is the key

EDITORIAL

An article on the concurrency into the world, do not miss to everyone with a concurrent programming appetizers, then we gradually over dinner, before eating dinner, I would like to quote those verses: “Wang Ling side, near and far different level, “far to see the profile in recent look at the details, constantly switching or thinking perspective to learning

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 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

Contents next article, we will talk about three issues caused by thread-safe: “Visibility, atomicity, ordering”, which involves a little content JMM, you can find out in advance so that we can better collision

Soul questioning

    Work in multithreaded programming scene more than you?

    Think of multi-threading, synchronized only think of it?

    Java and contracting each category, you have to understand the underlying implementation and design it?

Improve productivity tools


Recommended Reading

    Every day SpringBoot, still do not understand RESTful API returns a unified data format is how to achieve?

    Parents delegation model: manufacturers of high-frequency interview questions, easy to get

    Interview not know the difference between BeanFactory and the ApplicationContext?

    How to design a good RESTful API

    Red-black tree, super movement chart Detailed, easy to understand


Welcome to continued public concern number: “arch day a soldier.”

    Dry cutting-edge Java technology sharing

    Productivity tools summary | Reply “Tools”

    Analysis of interview questions and answers

    Technical Data collection | Reply “Information”

Easy to read detective novels thinking fun learning Java technology stack-related knowledge, in line with the simplification of complex issues, problems specific abstract and graphical decomposition of the principle of progressive technical issues, technology continues to update, please stay tuned ……

Leave a Reply