Java Double check lock

Source: Internet
Author: User

In Java programs, it may be necessary to postpone the initialization of some high -pin - like operations, and only when using these pairs of images only in -line initialization. This is called deferred initialization or lazy loading

Look at an unsafe deferred initialization:

After a thread executes 1, it discovers that the object instance is null, prepares for its new, and the B thread is new, which causes an error

We can make use of the sync lock to ensure correct:

But the overhead of synchronizing the entire method is too high, and people come up with double check locks:

The minimum range of synchronous locks used by double checks seems to accomplish the purpose, but there is a problem: when a thread 4 executes, thread B's 7 is not done yet, and thread A determines instance = null. Why does this happen when thread B's 7 is not done yet?

Take a look at the bottom-up key implementations of New Instance ():

In fact, 1 allocates memory first, then initializes the object and sets the instance. Then there are reflow, 2 and 3 in the order may be swapped:

So when B is done 7 o'clock, a in 4 determines that the instance object has been initialized, and if it is called before ctorinstance (memory), the instance will go wrong.

There are two solutions:

1. Declare the instance object as volatile, which disables the rearrangement of the 2,3

2. Using A class-based initialization solution: The JVM performs initialization of the class at the initialization stage of the class (that is, after class is loaded and before it is used by the thread). In

During initialization of the execution class, the JVM goes to fetch a lock. This lock can synchronize the initialization of multiple threads to the same class

We will find that the implementation code for the scenario based on class initialization is more concise. However, a volatile-based double-check locking scheme has an additional advantage: You can defer initialization of instance fields, in addition to delaying the initialization of static field implementations. Field lazy initialization reduces the overhead of initializing classes or creating instances, but increases the cost of accessing fields that are lazily initialized. Most of the time, normal initialization is better than lazy initialization. If you do need to use thread-safe deferred Initialization for instance fields, use the volatile-based deferred initialization scenario described above, and if you do need to use thread-safe deferred initialization for static fields, use the class-based initialization scenario described above.

Java Double check lock

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.