Defects and enhancements to the Java Threading/memory model

Source: Internet
Author: User

Java implements thread support at the language level. It provides a series of encapsulated classes and interfaces such as Thread/runnable/threadgroup, allowing programmers to efficiently develop Java multi-threaded applications. In order to achieve synchronization, Java provides the Synchronize keyword as well as the object's Wait ()/notify () mechanism, but behind the simple use, should hide the more complicated mystery, many problems is from this.

One, Java memory model

Before you learn about Java's Sync secrets, take a look at JMM (Java Memory Model).

Java is designed as a cross-platform language, and there is clearly a unified model for memory management. And the most important feature of the Java language is the abolition of pointers, freeing programmers from pain, regardless of memory usage and management problems.

Unfortunately, the world is not always satisfactory, although JMM designed to facilitate the programmer, but it increased the complexity of the virtual machine, but also led to some programming skills in the Java language failure.

The main purpose of JMM is to specify some relationships between threads and memory. For Java programmers, it is only the responsibility to use the Synchronized synchronization keyword, and other tedious tasks, such as data exchange/synchronization with threads/memory, are performed by the virtual machine. As shown in Figure 1: According to the design of JMM, the system has a main memory (main Memory), all variables in Java are stored in main storage and are shared for all threads. Each thread has its own working memory (Working Memory), working memory is stored in the main storage of some variables copy, the thread of all the variables are operating in the working memory, the thread can not be direct access to each other, variable transmission needs to be completed through main memory.

Figure 1 Example diagram of the Java memory model

To manipulate a variable, a thread must undergo a series of steps: first copying/Refreshing data from main memory to working memory, then executing code, referencing/assigning operations, and finally writing the variable contents back to main Memory. The Java Language Specification (JLS) defines 6 behaviors for threading and main memory interop, Load,save,read,write,assign and use respectively, which are atomic and interdependent and have a clear sequence of calls. For a specific description, see Chapter 17th JLS.

We introduced the role of synchronized in the previous chapters and now revisit the Synchronized keyword from a jmm perspective.

Assuming that a thread executes a synchronized code snippet, which operates on a variable, the JVM performs the following actions in turn:

(1) Get Sync Object Monitor (lock)

(2) Copying variables from main memory to current working memory (read and load)

(3) Executing the code, changing the value of the shared variable (use and assign)

(4) Using working memory data to refresh main memory related content (store and write)

(5) Free synchronization object Lock (Unlock)

Visible, synchronized's other role is to ensure that the main memory content and the thread in the work of the data consistency. If the Synchronized keyword is not used, the JVM does not guarantee that steps 2nd and 4th will be executed immediately in the order described above. Because according to the rules in JLS, the data exchange between the thread's working memory and main memory is loosely coupled, and when it is necessary to refresh the working memory or update the main memory content, it can be determined by the specific virtual machine. If multiple threads execute a piece of code that has not been synchronized protected at the same time, it is likely that a thread has changed the value of the variable, but other threads are unable to see the change, and are still operating on the old variable value, which ultimately results in unpredictable operations.

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.