Lazy loading of multithreaded singleton mode (lazy mode)

Source: Internet
Author: User

Package com.wz.thread.delay;
/**
* Lazy Load/Lazy mode single case design mode
* Using the DCL double check lock mechanism, DCL is the most multi-threaded combination of single-case mode using which solution
* @author Administrator
*
*/
public class MyObject {

Private volatile static MyObject MyObject;
Private MyObject () {}
public static MyObject getinstance () {
try {
if (myObject! = null) {

}else {
Thread.Sleep (3000);
Synchronized (Myobject.class) {
if (MyObject = = null) {
MyObject = new MyObject ();
}
}
}
} catch (Exception e) {
E.printstacktrace ();
}
return myObject;
}

}

Package com.wz.thread.delay;

public class MyThread extends Thread {

@Override
public void Run () {
Super.run ();
System.out.println (Myobject.getinstance (). Hashcode ());
}

}

Package com.wz.thread.delay;
/**
* The Hascode value of the output is the same, indicating that the same object
* @author Administrator
*
*/
public class Run {

public static void Main (string[] args) {
MyThread T1 = new MyThread ();
MyThread t2 = new MyThread ();
MyThread t3 = new MyThread ();
T1.start ();
T2.start ();
T3.start ();
}
}

Lazy loading of multithreaded singleton mode (lazy mode)

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.