Java single-Instance design pattern

Source: Internet
Author: User

What is a singleton design pattern

The singleton is only one instance, and the function of this pattern is to ensure that there is only one object for a class in the program.

The singleton model is divided into lazy and a hungry man type.

Lazy type
Class Student{static Student st;private Student () {}public static Student getinstance () {//Reference data type property in memory default value is null// If the value is null, only the object is created once if (st==null) {st = new Student ();} Return St;}} public class Test1 {public static void main (string[] args) {//Take advantage of Hascode equality is a singleton  Student tes1 = Student.getinstance (); S Ystem.out.println (Tes1.hashcode ()); Student tes2 = Student.getinstance (); System.out.println (Tes2.hashcode ());}}

Above you can see that the Hascode value of the result is the same as only one object was created

However, this pattern creates thread-safety problems in multi-threaded situations:

Class Teacher{static Teacher s;private Teacher () {}public static Teacher getinstance () {if (s = = null) {s = new Teacher ();} return s;}} Class Studentthread extends Thread{public void Run () {for (int i = 0;i<3;i++) {Teacher SS = Teacher.getinstance (); System.out.println (Ss.hashcode ());}}}  public class Test2 {public static void main (string[] args) {//Singleton mode there is a thread safety problem in multiple threads that  needs to be addressed thread safety//create thread studentthread TT1 = new Studentthread (); Studentthread tt2 = new Studentthread ();//Readiness Status Tt1.start (); Tt2.start ();}}

Hascode is not always the same if you run multiple times. This is a thread-safe issue:

Workaround you only need to set the method of the returned object to the synchronous method

Public synchronized static Teacher getinstance () {if (s = = null) {s = new Teacher ();} return s;}
A Hungry man type
A hungry man class hangle{//when creating a static property, it is assigned  and executed only once, which means that the  object is created only once, static hangle ha = new Hangle ();p ublic static Hangle GetInstance () {return ha;}} Class Singlethread extends Thread{public void Run () {for (int i = 0;i<4;i++) {hangle hh = hangle.ha; System.out.println (Hh.hashcode ());}}} public class Test3 {public static void main (string[] args) {Singlethread tt1 = new Singlethread (); Singlethread tt2 = new Singlethread ();//Readiness Status Tt1.start (); Tt2.start ();//Summary: Lazy-type thread safety issues in multithreaded environments (can solve threading problems)// And the A Hungry man-type, whether in single-threaded or multi-threaded thread-safe problems are  recommended to define a singleton mode with a hungry man//A Hungry man single-case mode than lazy-type efficiency}}

Java single-Instance design pattern

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.