Java concurrency,

Source: Internet
Author: User

Java concurrency,

I declare in advance that I am only a newbie to java concurrency. This article is just a summary and insights I have read the art of java concurrent programming (the content mainly involves the first three chapters. I hope you can discuss more about the errors.
0. Introduction
The world of programs is layered. Each layer encapsulates details and provides some methods or interfaces to provide functions, or even Constraint Functions in exchange for correctness. Next, we will use the layered idea as the soul and various memory models as the skeleton to briefly discuss the principles of Java concurrency.
1. processor memory model
The first question we face is what conditions should a good processor meet? Just like the students who take the test, of course they are doing fast and right. The processor generally meets two conditions: 1. Efficiency 2. correctness
Efficiency 1.1
For efficiency, the processor provides many mechanisms to meet this requirement, such as parallel processing, command assembly line, command re-arrangement, and cache mechanism. However, these methods must be restricted to ensure correctness, or to some extent, efficiency and correctness are mutually restricted. We must weigh them and make a reasonable compromise.
1.2 correctness
So how can we ensure correctness? In addition to some internal mechanisms of the processor, such as prohibiting some command shuffling, cache consistency, bus locks, cache locks, and so on, it also provides a good way to its upper layer, such as the memory barrier, CAS, etc. These methods and mechanisms will make the upper layer (in a reasonable way) better ensure the correctness of the program, just as Java will provide volatile, synchronized allows programmers (in a reasonable way) to ensure program thread security. Let's go into this topic more deeply. How can a program be called correct? What is the nature? To put it simply, there are three properties: 1. atomicity, that is, either completely or completely, requires hardware cooperation, such as CAS. 2. Visibility: because of the existence of the cache, to make the processing result of one processor visible to other processors, you must first write the cache into the memory. 3. The program must generate results in sequence. Note that many processor mechanisms can maintain a variety of features. For example, the memory barrier can refresh the cache into the memory, while the cache of other processors is invalid to ensure visibility, and prohibit command shuffling to ensure the order. For example, bus locks, cache locks, and cache consistency are ensured in various ways in order and atomicity.
2. java Memory Model
Okay, let's take it to the next level. We just said that the next layer (processor) provides various mechanisms to ensure efficiency and correctness, and this layer uses these mechanisms to reach this layer, that is, the correctness and efficiency of JRE and compiler. Like the processor model, JMM (java Memory Model) also has many mechanisms, including various optimizations. Volatile, synchronized, and the notorious concurrent package are also provided. Upper-layer programmers should also use (in a reasonable way) These Java languages and packages to ensure thread security. The following briefly describes the dependencies between the current and previous layers (note that synchronized can be divided into three types due to JMM optimization. final domain-> memory barrier 2. volatile-> memory barrier (bytecode level)-> memory barrier (processor level) 3. synchronized-> biased lock + lightweight lock + mutex lock-> CAS lightweight lock-> CAS mutex lock-> moniter-> atomic processor command 4. concurrent package-> volatile + CAS note that there may be three major problems in implementing atomic operations in the concurrent package in CAS: 1. ABA problems. (Solution: add the version number before the variable) 2. The cycle time is long and the sales volume is large. (Solution: pause command) 3. Only one atomic operation of shared variables can be guaranteed. (Solution: Use locks or merge operations) JMM abstracts various control program sequence mechanisms into text forms, that is, happens-before. The purpose is to shield Java developers from the underlying details of a specific platform. the correctness of the designed program can be guaranteed as long as it complies with happens-before.
3. Programmers
This is the top level. Simply put, it is to make good use of the mechanisms provided by the lower layer for better programming. I think this is why we need to learn the java concurrency principle.

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.