Java memory model JMM of multithreading concurrency

Source: Internet
Author: User

The introduction of multithreading concept is one of the embodiment of the effective compaction of the Zhai computer, and this is also very necessary, because the general operation of the process involves the reading of data, such as from disk, other systems, databases, CPU speed and data read speed has a serious imbalance, During the execution of a thread, a number of nodes will become blocked, making the calculation inefficient. In addition, the server side is the domain of Java best, as the server must be able to respond to multiple clients simultaneously request, also need multi-threading support. In the multi-threaded case, high concurrency will bring data sharing and competition, Tomcat as the middleware will be multi-threaded concurrency and other details as far as possible to encapsulate the processing, so that users of multi-threaded transparent, more attention to business processing. But if you want to dig deeper into tomcat, we need to learn more about multithreading concurrency, which describes some of the underlying principles of multithreading concurrency and the knowledge of the implementation of the JDK and the contract, and does not introduce the use of all specific concurrency classes. Some of the classes used in Tomcat are described in a little detail.

1. Java Memory model--JMM

In the multicore era, how to improve the performance of the CPU has become an eternal topic, and this topic is mainly about how to define a high-performance memory model, the memory model is used to define the processor's cache and shared memory synchronization mechanism and the rules of thread and memory interaction.

The Java world also has its own memory model, the Java memory model, or JMM. Since Java is defined as a cross-platform language, the description of the memory can also be cross-platform, and the Java Virtual machine attempts to define a unified memory model that encapsulates the memory access differences of various underlying hardware and operating systems. The Java program can achieve the same concurrency effect on different hardware and operating systems. It describes the relationships between variables in the program, including instance fields, static fields, data elements, and the underlying details of storing variables into memory and extracting variables from memory in the actual computer system.

In order to better understand the working mechanism of JMM, from the figure 2-5-1-1, there are several important subjects in the whole, main memory, working RAM, variable, variable copy, thread and so on. First of all, the main memory and the work and their relationship, main storage of Java program to save all the variables, of course, this variable does not include local variables and method parameters, while working memory contains copies of these variables, followed by the relationship between the thread and working memory, each thread has a working memory of its own, The working memory between the different threads is invisible to each other, and the operation of the thread on the variable is only for its own working memory, and the last is about the communication mechanism between threads, because the thread is directly not directly transitive, if a thread re-assigns a variable, then the other thread can only be known by the following way. The line Cheng changes the variable into main memory, and the line Cheng is read from primary memory, which basically completes the communication between the threads.

JMM defines the amount of eight operations to complete the communication between the working memory and main storage. If a thread prepares a new assignment for a variable, it may first lock a variable in main memory with a lock operation (not allowing other threads to get a lock on the variable until the lock of the variable is freed using the unlock operation), and then use the read operation to move the variable from main memory to the original Immediately after the load operation puts the value of the variable into the copy of the variable in the working memory, the use operation passes the value of the variable to the thread execution engine for operation, and the assign operation transfers the new variable value from the thread execution engine to the working memory, continuing down, The store operation transfers the value of the variable from the working memory into memory, then the write operation writes the resulting value to the corresponding variable in main memory, and finally releases the lock of the variable using the unlock operation.

Figure 2-5-1-1 Java memory model JMM

The Java memory model has three features: atomicity, Visibility, and ordering.

① Atomicity, the Java memory model guarantees that read, load, assign, use, store, write six operations have atomicity, and we can assume that in addition to the long and double types, access to the memory units corresponding to the other base data types is atomic. But because of this atomic particle size is too small, usually we need a larger particle size of the atom, then we need to use a lock to ensure.

② visibility, in the Java memory model, simply say that if a thread changes the value of a shared variable, and other threads can immediately know the change, we say that the variable has visibility. There are generally four ways to guarantee the visibility of variables, namely, volatile, synchronized, final, and lock. First of all, the volatile, the variables declared by this keyword, whenever there are any changes will be immediately synchronized to main memory, and each thread to use this variable will be re-flushing from main memory to work, so as to ensure the visibility of the variable (of course, the normal variable will eventually be synchronized to main memory, The main memory is synchronized to each thread of the working RAM, but this may eventually be more "long", can not guarantee visibility); Since the synchronized is also implemented by a lock, the essence of synchronized and lock is the same, when a thread releases a lock, Will force the value of the variable in the working memory to be flushed to memory, and when another thread acquires the lock, the value of the variable will be forced to reload, and of course the two threads get the same lock, which guarantees the visibility of the variable; Once the final declared variable is initialized, The final variable is visible to other threads. In fact, visibility can actually be seen as a mechanism by which a thread will send/receive changes to a variable when it enters/exits the synchronization block program.

③ order, order refers to the execution of the method within the thread, all the instructions are ordered, are executed in a serial manner, while the threads in the thread are observed, all instructions are unordered, and the instructions may cross-execute. Java provides the volatile and synchronized two keywords to ensure the order of the operation between threads, and this order is only relative, volatile prohibit command reordering, synchronized is guaranteed to hold the same lock synchronization block can only run serially.

JMM can be said to be the basis of Java, its definition will directly affect the JVM and Java Multi-threaded implementation of the mechanism, in order to understand the multi-threaded concurrency related problems, in-depth study of Java memory model is essential. Its definition must consider the following aspects, one is how to improve the performance efficiency of the thread more effectively; the other is how to shield the difference between the underlying physical hardware and the operating system to provide a unified external concept; Finally, how to make its model both rigorous and loose, to ensure that the semantics will not produce ambiguity and some optimization extension.




Multithreaded concurrency Java memory model JMM

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.