Java Singleton mode and java Singleton Mode

Source: Internet
Author: User

Java Singleton mode and java Singleton Mode

The Singleton mode is written in many books as follows:

public class SingleTon1 {private SingleTon1(){}private static SingleTon1 instance = null;public static SingleTon1 getInstance(){if(instance == null){instance = new SingleTon1();} return instance;}}

But it is not written in actual development. Because of a serious problem, multiple instances may be generated during multi-thread concurrent access !!

The following describes several common methods:

1. Use the synchronized keyword
Package singleton; public class SingleTon1 {private SingleTon1 () {} private static SingleTon1 instance = null; // multithreading solution 1, but the efficiency is not high! Because every call locks! Public static synchronized SingleTon1 getInstance () {if (instance = null) {instance = new SingleTon1 ();} return instance;} public void print () {System. out. println ("thread_id:" + Thread. currentThread (). getId ();} private static Object object = new Object (); // a clever method. The public static SingleTon1 getInstance2 () is not added after the lock is null () {if (instance = null) {synchronized (object) {instance = new SingleTon1 () ;}} return instance ;}}
2. Lock
Package singleton; import java. util. concurrent. locks. reentrantLock; public class SingleTon2 {private SingleTon2 () {} private static ReentrantLock lock = new ReentrantLock (); private static SingleTon2 instance = null; public void print () {System. out. println ("thread_id:" + Thread. currentThread (). getId ();} public static SingleTon2 getInstance2 () {if (instance = null) {lock. lock (); if (instance = null) {// note that You have to determine !! Instance = new SingleTon2 () ;}lock. unlock () ;}return instance ;}}
3. Use static variables:
Package singleton; public class SingleTon3 {public static void print () {System. out. println ("thread_id:" + Thread. currentThread (). getId ();} public static Nested getNested () {return Nested. instance;} // This is the class static class Nested {private Nested () {} static Nested instance = new Nested () ;}} created by the single instance ();}}

The above is a common mode for creating a singleton:

Test code:

Package singleton; import singleton. singleTon3.Nested; public class Test2 {public static void main (String [] args) {// TODO Auto-generated method stubNested singleton; Myrunnable mm = new Myrunnable (); myrunnable m1 = new Myrunnable (); Myrunnable 2 m2 = new Myrunnable2 (); new Thread (m1 ). start (); new Thread (m2 ). start (); if (m1.singleton = m2.singleton) {// is the same System. out. println ("the same");} else {System. out. println ("not the same") ;}} class Myrunnable implements Runnable {Nested singleton; @ Overridepublic void run () {// TODO Auto-generated method stubsingleton = SingleTon3.getNested (); singleTon3.print () ;}} class Myrunnable2 implements Runnable {Nested singleton; @ Overridepublic void run () {// TODO Auto-generated method stubsingleton = notify (); SingleTon3.print ();}}

Output:

Is the same
Thread_id: 11
Thread_id: 10

 

Related Article

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.