Implementation of Java single case schema data collation _java

Source: Internet
Author: User
Tags stub

The implementation of Java Single example mode , several methods of Java single example mode are sorted out:

Single case mode It's written in many books:

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

But the actual development is not so written, because there is a serious problem, multithreading concurrent access, may produce multiple instances!!

Here are a few common methods:

1. Use the Synchronized keyword

Package Singleton;
 
public class SingleTon1 {
   
   
  private SingleTon1 () {
     
  }
 
  private static SingleTon1 instance = null;
   
  Multithreading problem solution one, but not high efficiency! Because every call will lock! 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 = new Object ();
  The ingenious method is to add a lock on null and then not add public
  static SingleTon1 GetInstance2 () {
     
    if (instance = null) {
      Synchronized (object) {
        instance = new SingleTon1 ();
      }
    }
    return instance
  }
 
}

2. Add lock

Package Singleton;
 
Import Java.util.concurrent.locks.ReentrantLock;
 
public class SingleTon2 {
   
  private SingleTon2 () {
     
  }
  private static Reentrantlock lock = new Reentrantlock (); C6/>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 here also to judge!!
        instance = new SingleTon2 ();
      }
      Lock.unlock ();
    }
    return instance
  }
}

3. Using 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 a single instance of the class static classes created
  nested{
   private Nested () {
    }
  static Nested instance = new Nested ();
  }
}

These are the common patterns for creating a single example:

Test code:

 package singleton; Import Singleton.
 
singleton3.nested; public class Test2 {public static void main (string[] args) {//TODO auto-generated a stub Nested single
    ton
    myrunnable mm = new myrunnable ();
     
    Myrunnable m1 = new myrunnable ();
    Myrunnable2 m2 = new Myrunnable2 ();
    New Thread (M1). Start ();
    New Thread (M2). Start ();
    if (M1.singleton = = M2.singleton) {//Is the same System.out.println ("is the same");
    }else{System.out.println ("not the same");
      }} class Myrunnable implements runnable{Nested Singleton; 
        @Override public void Run () {//TODO auto-generated method Stub singleton = singleton3.getnested ();
      Singleton3.print ();
    } class Myrunnable2 implements runnable{Nested Singleton;
      @Override public void Run () {//TODO auto-generated method Stub singleton = singleton3.getnested ();
    Singleton3.print (); }
  }

Output:

is the same

Thread_id:11
Thread_id:10

Thank you for reading, I hope to help you, thank you for your support for this site!

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.