Double-check in Java)

Source: Internet
Author: User

The double check mode is mentioned in Article 48th in the book "Keep Itve Java", and it is pointed out that this mode is generally not applicable in Java. The structure of this mode is as follows:

 

Public resource getresource () {<br/> If (resource = NULL) {<br/> synchronized (this) {<br/> If (resource = NULL) {<br/> resource = new resource (); <br/>}< br/> return resource; <br/>}

 

This mode improves the following code:

Public synchronized resource getresource () {<br/> If (resource = NULL) {<br/> resource = new resource (); <br/>}< br/> return resource; <br/>}

 

The purpose of this Code is to delay Resource Initialization. However, synchronization is required for each access. To reduce synchronization overhead, a dual check mode is available.

In Java, the reason why the double check mode is invalid is that the reference type is not thread-safe without synchronization. The double check mode is applicable to the basic types except long and double.
. For example, the following code is correct:

Private int count; <br/> Public int getcount () {<br/> If (COUNT = 0) {<br/> synchronized (this) {<br/> If (COUNT = 0) {<br/> COUNT = computecount (); // a time-consuming calculation <br/>}< br/> return count; <br/>}

 

The preceding section describes the double-check mode in Java.
Idiom. But it is not over yet, because the memory mode of Java is also being improved. Doug
Lea wrote in his article: "according to the latest jsr133 Java memory model, if you declare the reference type as volatile, the dual-Check Mode will work.", see
Http://gee.cs.oswego.edu/dl/cpj/updates.html
.

Therefore, to use the Double Check Mode in Java in the future, you can use the following code:

 

Private volatile resource; <br/> public resource getresource () {<br/> If (resource = NULL) {<br/> synchronized (this) {<br/> If (resource = NULL) {<br/> resource = new resource (); <br/>}< br/> return resource; <br/>}

 

Of course, it must be in Java that complies with the jsr133 specification.

Therefore, double-check is unavailable in j2se 1.4 or earlier versions due to out-of-order writes during multi-thread or JVM optimization.
This problem has been fixed in j2se 5.0. You can use the volatile keyword to ensure the singleton under multiple threads.

Public class Singleton {<br/> private volatile Singleton instance = NULL; <br/> Public Singleton getinstance () {<br/> If (instance = NULL) {<br/> synchronized (this) {<br/> If (instance = NULL) {<br/> instance = new Singleton (); <br/>}< br/> return instance; <br/>}< br/>

 

Recommended Methods
Is Initialization on demand holder (iodh ),

See http://en.wikipedia.org/wiki/Initialization_on_demand_holder_idiom

 

Public class Singleton {<br/> static class singletonholder {<br/> static Singleton instance = new Singleton (); <br/>}</P> <p> Public static Singleton getinstance () {<br/> return singletonholder. instance; <br/>}< br/>

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.