Single case Mode &synchronized

Source: Internet
Author: User
Package COM.IBM.DG;
        
        public class Main {public static void main (string[] args) {//TODO auto-generated method stub
        Singletonclass s1 =singletonclass.getinstall ();
        
        Singletonclass s2 = Singletonclass.getinstall ();   System.out.println (S1==S2);  True System.out.println (s1.equals (S2));
True}}//single case mode scenario and Opportunity:///////////////////////////////

The resource pool creates the connection, the database connects, but the general database all uses the connection pool, the single example pattern can cause waits package com.ibm.dg; public class Singletonclass {private Singletonclass () {}//define private constructor method private static Singletonclass Install=nul
    L public static Singletonclass Getinstall () {if (install = null) {synchronized (Singletonclass.class) {// Synchronized when two concurrent threads access the synchronized (this) synchronized code block in the same object,//Only one time Threads are executed.
                Another thread must wait until the current thread finishes executing the code block before executing the code block.
                if (install = = NULL) {install = new Singletonclass ();
      }      } return install;

 }
}


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.