Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use singleton mode in Java

Shulou Source: shulou.com Published: 2022-06-01 11:58:41 10月04日 Update

This article is about how the singleton pattern is used in Java. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

Be careful

1. A singleton class can only have one instance.

2. The singleton class must create its own unique instance.

3. The singleton class must provide this instance to all other objects.

Advantages

1. Having only one instance in memory reduces memory overhead, especially the frequent creation and destruction of instances (such as managing the academy home page cache).

two。 Avoid multiple occupations of resources (such as writing to files).

Shortcoming

1. There is no interface, can not be inherited, in conflict with the principle of single responsibility, a class should only care about internal logic, not how to instantiate outside.

Working with scen

1. A unique serial number is required for production.

The counters in 2.WEB do not have to be added to the database every time they are refreshed, but are cached first with a single example.

3. The creation of an object consumes too many resources, such as the connection between Imax O and the database.

First, the implementation method package com.asurplus.common.singleton.style1;import lombok.extern.slf4j.Slf4j;import java.util.Objects;@Slf4jpublic class ResUtils {private volatile static ResUtils instance = null / * Private constructor * / private ResUtils () {} / * provides the method to obtain instances * * @ return * / public static ResUtils getInstance () {/ / create if (Objects.isNull (instance)) {/ / avoid concurrent operations for empty talent Synchronized (ResUtils.class) {/ / create if (Objects.isNull (instance)) {/ / create a new object instance = new ResUtils () Log.info ("object created");} return instance;}}

We privatize its constructor so that an instance cannot be created externally, and we provide a way to get a unique instance so that we can get it externally.

2. Implementation method package com.asurplus.common.singleton.style2;import lombok.extern.slf4j.Slf4j;@Slf4jpublic class ResUtils2 {/ * static inner class * / private static class ResUtils2Holder {private static ResUtils2 instance = new ResUtils2 ();} / * provide the method to obtain instances * * @ return * / public static ResUtils2 getInstance () {return ResUtils2Holder.instance;}}

We use the static inner class method to create an instance, because the principle that JVM will only load once will eventually create only one instance and provide a way to get the instance so that we can get it from the outside.

3. Test package com.asurplus.common.singleton;import com.asurplus.common.singleton.style1.ResUtils;import java.util.concurrent.ExecutorService;import java.util.concurrent.Executors;/** * singleton mode * / public class TestMain {public static void main (String [] args) {/ / create thread pool ExecutorService executorService = Executors.newFixedThreadPool (10); for (int I = 0; I < 100) ExecutorService.shutdown +) {executorService.execute (ResUtils::getInstance);} executorService.shutdown ();}}

Output result

As you can see, we got 100 instances and created only one instance, thus implementing our singleton pattern.

Thank you for reading! This is the end of the article on "how to use the singleton pattern in Java". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it for more people to see!

Tags: Instances methods patterns objects memory content data databases methods more articles caches resources static private good practical frequent not used advantages Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi Linux Docker Huawei Microsoft