Multi-thread concurrency-java Memory Model JMM

Source: Internet
Author: User

Multi-thread concurrency-java Memory Model JMM

The introduction of the concept of Multithreading is another embodiment of the effective computation of humans, and this is also very necessary, because data reading is involved in the general operation process, for example, from disks, other systems, databases, and so on, there is a serious imbalance between the computing speed of the CPU and the Data Reading Speed. During this period, if one thread is executed, congestion will occur on many nodes, low computing efficiency. In addition, the server is the best field in java. As a server, it must be able to respond to requests from multiple clients at the same time, and also require multi-thread support. In the case of multithreading, high concurrency will bring about data sharing and competition issues. As a middleware, tomcat encapsulates details such as multithreading concurrency as much as possible to make users transparent to multithreading, focus more on business processing. However, if you want to deeply analyze tomcat, it is necessary to have a deep understanding of the multi-thread Concurrency Technology. This chapter will introduce some underlying principles of multi-thread concurrency and the implementation of JDK concurrent package, the usage of all specific concurrent classes will not be introduced, but some classes used in Tomcat will be described in a little detail.

1. Java Memory Model-JMM

In the multi-core era, how to improve CPU performance has become an eternal topic. The main topic is how to define a high-performance memory model, the memory model is used to define the synchronization mechanism between cache and shared memory at each layer of the processor and the rules for interaction between threads and memory.

The world of Java also has its own Memory Model, the Java Memory Model, namely the Java Memory Model, JMM for short. Java is defined as a cross-platform language, so the memory description must be cross-platform. the Java Virtual Machine attempts to define a unified memory model, it can encapsulate the memory access differences between various underlying hardware and operating systems, so that Java programs can achieve the same concurrency effects on different hardware and operating systems. It describes the relationships between variables in a program, including instance domains, static domains, data elements, and the underlying details of storing variables in memory and getting them from memory in a computer system.

To better understand the working mechanism of JMM, it is introduced from Figure 2-5-1-1. As a whole, there are several important subjects, including primary memory, working memory, variables, variable copies, and threads. First, let's look at the relationship between the primary memory and the working memory. The primary memory stores all the variables of the java program. Of course, this variable does not include local variables and method parameters, the working memory contains copies of these variables, followed by the relationship between the thread and the working memory. Each thread has its own working memory, the working memory of different threads is invisible to each other, and the operations performed by the thread on variables can only be performed on their own working memory. Finally, it is about the communication mechanism between threads, since the thread cannot be directly transmitted, if a thread re-assigns a value to a variable, it can only be known to another thread through the following channels. Thread 1 reflects the variable change to the primary memory, thread 2 then reads data from the main memory, which basically completes the communication between threads.

JMM defines eight operations to complete the communication between the working memory and the primary memory. If a thread is about to assign a value to a variable, it may first lock a variable in the main memory with the lock operation (so that other threads will not get the lock of this variable, until the unlock operation is used to release the lock of the variable), then the read operation is used to save the variable from the master memory, and then the load operation puts the obtained variable value into the copy of the variable in the working memory, the use operation transmits the variable value to the thread execution engine for operation. The assign operation transfers the new variable value from the thread execution engine to the working memory and continues, the store operation transfers the variable value from the working memory to the primary memory. Then, the write operation writes the obtained value to the corresponding variable of the primary memory, and finally releases the variable lock using the unlock operation.

<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHAgYWxpZ249 "center"> Figure 2-5-1-1 java Memory Model JMM

The Java memory model has three features: atomicity, visibility, and orderliness.

① Atomicity. the java memory model ensures the atomicity of the read, load, assign, use, store, and write operations. We can consider that apart from the long and double types, the access and read operations to memory units corresponding to other basic data types are atomic. However, because the atomicity granularity is too small, we usually need more atomicity. In this case, we need to use a lock to ensure it.

② Visibility: in the java memory model, if a thread changes the value of the shared variable and other threads can immediately know the change, we will say that the variable is visible. Generally, there are four ways to ensure variable visibility: volatile, synchronized, final, and lock. First, let's talk about volatile. variables declared by this keyword will be immediately synchronized to the main memory whenever any changes are made, and every thread needs to refresh from the main memory to the working memory when using this variable, this ensures the visibility of variables (of course, normal variables will eventually be synchronized to the primary memory, and then the primary memory will be synchronized to the working memory of each thread, but this may eventually be "long ", visibility is not guaranteed); Because synchronized is implemented through locks at the underlying layer, synchronized and locks are essentially the same. When a thread releases a lock, the variable value in the working memory is forcibly refreshed to the primary memory, and the variable value is forcibly reloaded when another thread obtains the lock, of course, the two threads obtain the same lock, which ensures the visibility of the variable. Once the final declared variable is initialized, other threads can see this final variable. In fact, visibility can be seen as a mechanism. When a thread enters/exits the synchronization block program, it will send/receive a variable change.

③ Orderliness refers to the execution of methods in the thread. All commands are ordered and executed in a serial mode, while other threads are observed in the thread, all commands are unordered and may be executed in parallel. Java provides volatile and synchronized keywords to ensure the operation orderliness between threads. This orderliness is only relative. volatile prohibits Command Re-sorting, synchronized ensures that the synchronization block holding the same lock can only run in serial mode.

JMM can be said to be the basis of Java. Its definition will directly affect the JVM and java multi-threaded implementation mechanisms. If you want to have a deep understanding of the symptoms related to multi-threaded concurrency, in-depth research on the Java memory model is essential. Its definition must consider the following aspects: first, how to improve the thread performance efficiency more effectively; the second is how to eliminate the differences between underlying physical hardware and operating systems to provide a unified external concept, and finally how to make its model rigorous and relaxed, make sure that the semantics does not produce ambiguity and some optimization extensions.




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.