About Java single-instance object synchronization issues

Source: Internet
Author: User

A single Example object (Singleton) is a common design pattern. In Java applications, a singleton object can guarantee that only one instance of the object exists in a JVM. It is precisely because of this feature that a single object is usually used as a carrier for storing configuration information in a program because it guarantees that other objects will read consistent information. For example, in a server program, the configuration information for the server may be stored in a database or file, which is read uniformly by a single instance object, and other objects in the service process need to access the single Instance object if they want to get the configuration information. This approach greatly simplifies configuration management in complex environments, especially in multi-threaded environments, but can also bring some synchronization problems as the application scenario is different.

This article will explore several synchronization problems that may occur when using a single example object for configuration information management in a multithreaded environment, and give an alternative solution to each problem.

Problem description

In multithreaded environment, the problem of synchronization of single object is mainly embodied in two aspects, the initialization of single case object and the property update of single object.

The methods described in this article have the following assumptions:

1. The acquisition of a single Instance object's attributes (or member variables) is achieved through the initialization of a single instance object. That is, when a single instance object is initialized, the most recent configuration information is read from a file or database.

2. Other objects cannot directly change the properties of a single instance object, and the change in a single Instance object's properties comes from changes in the configuration file or metabase data.

1.1 Initialization of a single Instance object

First, we discuss the initialization synchronization of a single Instance object. The usual way to handle a single case pattern is to there is a static member variable in the object, the type is the single instance type itself, and if the variable is null, the object of the single instance type is created and the variable is directed to that object, and if the variable is not NULL, the variable is used directly.

The procedure is as shown in the following code:

public class GlobalConfig {
   private static GlobalConfig instance = null;
   private Vector properties = null;
   private GlobalConfig() {
    //Load configuration information from DB or file
    //Set values for properties
   }
   public static GlobalConfig getInstance() {
    if (instance == null) {
     instance = new GlobalConfig();
    }
    return instance;
   }
   public Vector getProperties() {
    return properties;
   }
  }

This approach works well in a single-threaded mode, but in multithreaded mode, it can cause problems. If the first thread discovers that the member variable is NULL, prepare to create the object; This is the second thread that also discovers that the member variable is null and creates a new object. This can result in instances of multiple singleton types in a JVM. If the member variable of this singleton type changes in the course of the operation, it will result in inconsistency of multiple singleton type instances, creating some very strange phenomena. For example, a service process stops multiple thread services by examining one of the properties of a singleton object, and if there are instances of multiple singleton objects, it can cause some thread services to stop and some thread services cannot stop.

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.