Immediate loading of multithreaded singleton mode (a Hungry man mode)

Source: Internet
Author: User

Package com.wz.thread.immediately;
/**
* Load now/A Hungry man mode single case design mode
* @author Administrator
*
*/
public class MyObject {

private static MyObject MyObject = new MyObject ();
Private MyObject () {}
public static MyObject getinstance () {
return myObject;
}

}

Package com.wz.thread.immediately;

public class MyThread extends Thread {

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

}

Package com.wz.thread.immediately;
/**
* 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 ();
}
}

Immediate loading of multithreaded singleton mode (a Hungry man 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.