Java Concurrency programming 16th Chapter Java memory model

Source: Internet
Author: User
Tags volatile

What is a memory model
  • JMM ( Java memory model) specifies the JVM a minimum set of guarantees must be followed, which specifies when write operations to variables will be visible to other threads.
  • jmm Defines a partial-order relationship for all operations in the program, called happens-before happens-before relationship, then Span style= "font-family: Microsoft Jas Black" >JVM

    Happends-before rules include:

    1. Program Order rules. If action A in the program is before Operation B, then action A in the thread is performed before Operation B.

    2. Monitor lock rule. The unlock operation on the same monitor must be performed before the lock operation. ,

    3. Volatile variable rules. The write operation to the volatile variable must be performed before the read operation.

    4. Thread initiation rules. Thread.Start must be called before an operation is performed in the thread.

    5. Thread shutdown rules. Any action in the thread must be executed before other threads detect the end of the thread, or return from Thread.Join, or call threas.isalive to return false.

    6. Break rules. When a thread calls interrupt on another thread, it must be executed before the interrupt call is detected by the interrupted thread.

    7. Finalizer rules. The constructor of the object must be completed before the finalizer of the object is started.

    8. transitivity. If operation A is performed before action B, action B is performed before Operation C, then action A must be performed before Operation C.

    Double check lock

  1. /**
  2. * Double check plus lock, unsafe,
  3. * Threads may see invalid values, plus volatile modifiers
  4. */
  5. public class Doublecheckedlocking {
  6. private static Object resource;
  7. Public static Object getinstance () {
  8. if (resource = = null) {
  9. synchronized (doublecheckedlocking.class) {
  10. if (resource = = null) {
  11. Resource = new Object ();
  12. }
  13. }
  14. }
  15. return resource;
  16. }
  17. }

If you do not use volatile, you may see an error or an invalid state for the object.

Because next to see the "Java Concurrent programming Art", there are a lot of chapters on the memory model of the only, and this chapter of the book is more obscure, so do not look closely.

Java Concurrency programming 16th Chapter Java memory model

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.