Java Singleton Mode)

Source: Internet
Author: User

Java Singleton Mode)
Definition: The Singleton mode ensures that only one instance of a Class exists in a Java application. In many operations, such as creating a directory database connection, such single-threaded operations are required. In addition, singleton can be stateful. In this way, multiple single-State classes can serve as a status warehouse together. For example, if you want a Post Counter in the Forum, you need to count every time you browse. Can single-State classes maintain this count and automatically add 1 to synchronize's security? If you want to save this number to the database permanently, you can easily do this without modifying the single-State interface. In addition, Singleton can also be stateless. The Singleton mode provides tool-based functions, which makes it possible for us to achieve this. Singleton also saves memory because it limits the number of instances and facilitates Java garbage collection ). We often see that the class loader in the factory mode is also implemented in the Singleton mode, because the loaded class actually belongs to resources. How to Use Singleton mode. Generally, Singleton mode has several forms: public class Singleton {private Singleton () {}// define your own instance in your own, isn't it strange? // Note that this is private only for internal calls. private static Singleton instance = new Singleton (); // a static method is provided for external access to this class, you can directly access public static Singleton getInstance () {return instance ;}} In the second form: public class Singleton {private static Singleton instance = null; public static synchronized Singleton getInstance () {// This method is better than above. You do not need to generate objects every time. It is only the first time you use this method to generate an instance, which improves the efficiency! If (instance = null) instance = new Singleton (); return instance ;}} use Singleton. getInstance () to access the single-State class. The second form above is lazy initialization. That is to say, the initial Singleton will not be regenerated in the first call. Note that synchronized in the form of lazy initialization is important. If synchronized is not available, you may obtain multiple Singleton instances by using getInstance. There are many discussions about Singleton of lazy initialization involving double-checked locking (DCL), which will be further studied by interested parties. It is generally considered that the first form is more secure. Note: In Singleton mode, Singleton cannot be used in some cases. If multiple Singleton objects are simultaneously loaded by different classloaders; in distributed systems such as ejbs, you should also pay attention to this situation because ejbs are cross-server and cross-JVM. Let's take ServiceLocator of Pet Store source code (Pet Store 1.3.1) of SUN as an example: ServiceLocator in Pet Store has two types: ServiceLocator under EJB directory and WEB directory, when we check the two ServiceLocator services, we will find that the content is similar. They all provide the query and positioning service of EJB. Why should we separate them? After careful research on the two ServiceLocator methods, we can find the difference: ServiceLocator in the WEB adopts the Singleton mode, and ServiceLocator is a resource location. Naturally, we should use the Singleton mode. However, in EJB, the Singleton mode has no effect, so ServiceLocator is divided into two types: WEB Services and EJB services. The Singleton mode looks simple and easy to use, but it is not easy to use well. You need to have a good understanding of Java's class thread memory and other concepts.

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.