[Java Basics] Singleton mode, java Basics

Source: Internet
Author: User

[Java Basics] Singleton mode, java Basics

Singleton mode: ensure that one or only one object exists during the entire project operation.

Mainly include: 1, hunger, 2, lazy.

1 class Singleton1 // 2 {3 private Singleton1 () {}// privatize the constructor first, the external creation object 4 private static final Singleton1 instance = new Singleton1 () is not allowed; 5/* The hungry Chinese expression is assigned a value of 6 when it is declared. The disadvantage is that space needs to be opened during loading, performance Loss is required and loading is slow. 7 */8 public static Singleton1 getInstance () 9 {10 return instance; 11} 12} 13 class Singleton2 // lazy 14 {15 private Singleton2 () {} 16 private static Singleton2 instance; // = null17/* the lazy statement is that no value is assigned during the Declaration. The disadvantage is that the thread is not secure. 19 */20 public static Singleton2 getInstance () 21 {22 if (instance = null) 23 instance = new Singleton2 (); 24 return instance; 25} 26} 27 class SingletonDemo 28 {29 public static void main (String [] args) 30 {31 Singleton1 s1 = Singleton1.getInstance (); 32 Singleton1 s2 = Singleton1.getInstance (); 33 System. out. println (s1 = s2); 34 35 Singleton2 s3 = Singleton2.getInstance (); 36 Singleton2 s4 = Singleton2.getInstance (); 37 System. out. println (s3 = s4); 38} 39}

I am a newbie, and the above is a summary of my online self-learning experience. If you have any mistakes, please note. There are a lot of new kids shoes to talk about, and great gods give more advice. I wish you a happy life.


Java Singleton mode? How to Write

Class AAA {
Private static AAA a = null;
Private AAA (){

}
Public static AAA getaskact (){
If (= null ){
A = new AAA ();
}
Return;
]
}

The key is the private constructor, and then an entry that can get the object.

How to write the code in java Singleton mode?

I have pasted my article from my blog. I should have a clear explanation of the singleton mode:
The Singleton mode is very common in our daily projects. When we need such an object in the project, this object can only have one instance in the memory, in this case, we need to use a singleton.

Generally, the singleton mode includes the following:

1. Hunger-type Singleton

Public class Singleton {
Private Singleton (){};
Private static Singleton instance = new Singleton ();
Public static Singleton getInstance (){
Return instance;
}
}

This is the simplest Singleton, which is the most common and reliable! Its only drawback is that it cannot complete delayed loading-that is, when the system has not used this Singleton, the singleton will be loaded into the memory.
Here we can perform a test like this:

Modify the above Code:

Public class Singleton {
Private Singleton (){
System. out. println ("createSingleton ");
};
Private static Singleton instance = new Singleton ();
Public static Singleton getInstance (){
Return instance;
}
Public static void testSingleton (){
System. out. println ("CreateString ");
}
}

We test it in another test class (in this example, all tests passed Junit)

Public class TestSingleton {
@ Test
Public void test (){
Singleton. testSingleton ();
}
}

Output result:

CreateSingleton
CreateString

We can note that in this Singleton, even if we do not use the singleton class, it is still created. This is of course what we do not want to see, so we have the following Singleton.

2. Lazy Singleton

Public class Singleton1 {
Private Singleton1 (){
System. out. println ("createSingleton ");
}
Private static Singleton1 instance = null;
Public static synchronized Singleton1 getInstance (){
Return instance = null? New Singleton1 (): instance;
}
Public static void testSingleton (){
System. out. println ("CreateString ");
}
}

Synchronization is required when the preceding Singleton obtains an instance. If synchronization is not added, when thread 1 completes the create Singleton operation in a multi-threaded environment, before assigning values, thread 2 may judge
The disconnected instance is empty. At this time, thread 2 also exists.

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.