Java single-state mode (singleton mode)

Source: Internet
Author: User

Definition: The main function of the singleton mode is to ensure that only one instance of a class is present in a Java application.

Such a single-threaded operation is required in many operations, such as establishing a directory database connection.

Also, Singleton can be stateful, so that multiple single-state classes together can be as a state warehouse to provide services, for example, you want to forum posts counter, each time you need to count, the single-state class can keep this count, and can synchronize security auto plus 1 , if you want to persist this number to the database, you can do it conveniently without modifying the single interface.

In addition, Singleton can be stateless. Provides tool-type functionality,

The singleton model provides us with the possibility of this implementation. The advantage of using singleton is that it can save memory because it limits the number of instances and facilitates Java garbage collection (garbage collection).

We often see that the class loader in the Factory mode is also implemented in the singleton mode, because the loaded class is actually a resource. How to use the Singleton mode General singleton mode usually has several forms:
 public  class   Singleton {  Singleton () {}  //  Is it strange to define an instance of itself within itself?  //  Note this is private only for internal calls  private  static  Singleton instance = new   Singleton ();  //  public  static   Singleton getinstance () { return   instance; }}

The second form of:
 Public classSingleton {Private StaticSingleton instance =NULL; Public Static synchronizedSingleton getinstance () {//This method is better than the above, and does not have to be generated every time, just the first time//generate an instance when used, improve the efficiency!       if(instance==NULL) instance=NewSingleton (); returninstance; }}


Use Singleton.getinstance () to access a single-state class.

The second form above is the lazy initialization, which means that the initial singleton is first invoked, and no more generation is needed.

Note that synchronized in the form of lazy initialization, this synchronized is important, if there is no synchronized, then using getinstance () is possible to get multiple singleton instances. There are many discussions about the singleton of the lazy initialization that involve double-checked locking (DCL), and interested persons are further studied.

The first form is generally considered to be more secure. Considerations for using singleton mode sometimes in some cases, using singleton does not achieve the purpose of Singleton, such as having multiple singleton objects loaded by different class loaders at the same time, and in distributed systems such as EJBS, because EJBs are cross-server , across the JVM.

We take a look at the servicelocator of Sun's Pet shop source (pet store 1.3.1) for a little analysis:

There are two kinds of servicelocator in the pet store, one is the EJB directory, the other is the Web directory, we check these two servicelocator will find the content is similar, are to provide EJB query location service, but why should separate it? Careful study of the two servicelocator only to find the difference: Servicelocator in the web to take the singleton model, Servicelocator belongs to the resource location, it is natural to use singleton mode. However, in EJB, Singleton mode has lost its function, so servicelocator is divided into two kinds, one for Web service and one for EJB service.

The singleton pattern looks simple and easy to use, but it's not easy to really use it, and it requires a good understanding of Java's class thread memory concepts.

Java single-state mode (singleton 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.