Java Concurrency programming 16th Chapter Java memory Model reading notes

Source: Internet
Author: User


The Java memory model is the foundation for multi-threaded security, but the understanding of the cognitive type is not studied in depth.
First, what is the memory model, why it needs its Java memory model (Java storage models) concurrency-related security announcements, synchronization policy specification, consistency, etc. are from JMM.
1 The memory model of the platform tells the application how to be guaranteed from the memory system in the schema-defined memory model, and also defines special instructions (called Memory fences or fences) that enable additional storage coordination guarantees when data is shared. The JVM masks the difference between the JVM and the underlying platform memory model by inserting a memory fence at the appropriate location.
2 Reorder multiple threads when you operate the same variable at the same time, read and write must guarantee atomicity, otherwise the value of the variable being read will not be the most recent change in the reordering problem.
In-depth understanding of the Java Memory Model (ii)--reordering


3 Java memory Model A brief introduction to Happens-before's rules includes:
Program order rules. Assuming that action a in the program precedes Operation B, the a operation in the thread will run before the B operation.
Monitor lock rules. The unlock operation on the monitor lock must be run before a lock operation on the same monitor.
Volative variable rules. The write operation to the volative variable must be run before the read operation on the variable.
Thread start rule. Calls to Thread.Start on the thread must run in the thread before whatever operation.
Thread end rule. Any operation in the thread must be run before other threads have checked to the end of the thread, or return false from Thread.Join, or when the threas.isalive is called.
The interrupt rule. When interrupt is called on a thread thread is waiting, it must be run before the interrupted thread detects the interrupt call (by throwing interruptedexception, or by calling isinterrupted and interrupted).
The finalizer rule. The constructor of the object must be run before the finalizer of the object is started.
Transitivity. Assuming that action a runs before Operation B, and that Operation B runs before Operation C, operation A must run before Operation C.
In-depth understanding of the Java memory Model (i)--basic


4 other Happens-before ordered in the class library with synchronization include:
Putting an element into a thread-safe container will run before another thread obtains the element from the container.
The countdown operation on the Countdownlatch will run before returning the thread from the await method on the latch.
Releasing the semaphore license will run before a license is obtained from the semaphore.
The entire operation of the task represented by the future will run before it is returned in Future.get.
An operation that submits a runnable or callable to executor will run before the task starts to run.
A thread that arrives at Cyclicbarrier or exchanger will run before other threads that arrive at the fence or switch point are freed. Assuming that the cyclicbarrier uses a fence operation, the operation that arrives at the fence will run before the fence operation, and the fence operation will run before the thread is released from the fence.
Ii. Publication of
1 unsafe announcements In addition to immutable objects, it is generally unsafe to use objects that are initialized with one thread, unless the object's advertised operation is run before the thread that uses the object starts using it.
2 Security Announcement
3 Safe Initialization mode
4 double check plus lock
Iii. security in the process of initial initialization

References: In-depth understanding of Java memory Models
"In-depth understanding of Java Virtual machines: JVM advanced features and best practices" part fifth efficient concurrency

Java Concurrency programming 16th Chapter Java memory Model reading notes

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.