Make writing a habit together! This is the 15th day of my participation in the “Gold Digging Day New Plan · April More text Challenge”. Click here for more details

This series of columns Java concurrent Programming column – Yuan Hao 875 column – Nuggets (juejin. Cn)

preface

Concurrent programming is not a relatively independent discipline, but a comprehensive discipline, which involves a lot of knowledge points, this article first describes the core and essence of concurrent programming.

The body of the

No matter what knowledge points to learn, the most taboo is “blind men and elephant”, there is a very good learning method called “jump out, see the panorama” and “drill in, see the essence”, although a lot of concurrent programming knowledge, but can be abstract for three core issues: division of labor, synchronization and mutual exclusion.

Division of labor

The so-called division of labor, similar to the reality you are a project manager, you need to organize to complete a project, need to divide these tasks, arrange appropriate personnel to do; In concurrent programming, employees are like threads, and you need to allocate tasks to threads because it directly determines the performance of concurrent programs.

There are many SDKS in Java that divide tasks, such as Executor, Fork/Join, which is essentially a division method, and producer-consumer design patterns, all of which are ideas about how to distribute a task reasonably.

synchronous

In project execution, tasks are dependent on each other. For example, after the requirement review, developers are required to start coding. How can we inform the follow-up task to start coding?

In concurrent programming, it mainly refers to the cooperation between threads, and there is no difference in real life, that is, when a thread completes a task, how to notify the subsequent task threads to start.

For example, a Future can make an asynchronous call. When the main thread gets the result through get(), the main thread waits, and when the asynchronous result returns, the Get () method automatically returns. The Future utility class does this for me.

The thread cooperation problem in work can be summarized as: when a condition is not met, the thread needs to wait, when a condition is met, the thread needs to be awakened to execute. For example, in the producer-consumer model, when the queue is full, the producer thread waits, when the queue is not full, the producer thread needs to wake up to execute; When the queue is empty, the consumer thread waits, and when the queue is not empty, the consumer thread needs to be awakened to execute.

The mutex

Division of labor, synchronization is primarily about performance, but in concurrent programs there is also a part about correctness, known in the jargon as “thread safety.”

In concurrent programs, when multiple threads access a Shared variable at the same time, the result is uncertain, which lead to uncertainty is the main source of the visibility problem, orderliness and atomicity problems, in order to solve the three problems, the Java language memory model is introduced, and the memory model provides a series of rules, using these rules, Visibility and order can be avoided, but it is not enough to solve the problem of thread safety, the core solution to solve thread safety is mutually exclusive.

Mutexes allow only one thread to access a shared variable at a time.

Lock is the core technology to achieve mutual exclusion. Synchronized in Java and various locks in SDK can solve the mutual exclusion problem, but also bring performance problems, which should be optimized according to the situation and the scene. For example, ReadWriteLock and StampedLock can optimize the scene with more read and less write. You can also use a lock-free data structure called an atomic class.

conclusion

Now that we’ve “jumped out and seen the big picture”, we can understand the core and nature of concurrent programming, and we’ll continue to “dive in and see the big picture” in the rest of this article.