Reprinted please indicate the source: http://blog.csdn.net/sinyu890807/article/details/8860649
When writing software, you often need to use the log printing function, which can help you debug and locate problems. After the project is launched, it can also help you analyze data. However, Java Native systems. out. the println () method is rarely used in real project development. Even code check tools such as findbugs think that system is used. out. prin
Concept: In Java, the singleton pattern is a common design pattern, there are several types of single-instance pattern, here are 2 kinds: Lazy Type single case, a hungry man type single case.The singleton mode has the following characteristics:1. A singleton class can have only one instance.2. The
The singleton pattern in Java is a widely used design pattern and is one of 23 design patterns in Java. The role of Singleton mode is to ensure that in a Java program, only one program exists for a class.Here is a brief introduction to its two basic ways: a hungry man-style
The following is an implementation of the singleton mode.Public class station {Private Static station ST = new station ();Private int num = 10;Private station (){}Public static station getinstance (){Return st;}Public void upload (){If (St. Num> 0 ){Num --;System. Out. println ("with tickets ");} Else {System. Out. println ("tickets sold out ");}}}
Public class demo {
Public static void main (string [] ARGs ){
Station ST = station. getinstance ();For
Single-Case mode:Ensure that a class has only one instance and provides a global access pointA Hungry Man: (Thread-safe)public class Singleton {private static Singleton uniqueinstance = new Singleton ();Private Singleton () {}public static Singleton getinstance () {return un
Static voidTest () {System.out.println ("Test invoked."); }}Note: This solves the problem of synchronization, but when the static test () method is called or the static variable i is called, even if you do not need to use the Singleton object, he is already loaded into memory .4. The recommended wording is as follows: using an internal class approach, the JVM guarantees that its member variables are initialized and thread-safe when used with internal
1. Lazy type Single case/*** Lazy Single case: the most common way of writing * Disadvantage: There will be problems in multithreaded situations, so when multithreading is usually preceded by the getinstance () method with the Synchronized keyword*/classlazysingleton{Private StaticLazysingleton Lazysingleton =NULL; PrivateLazysingleton () {} Public StaticLazysingleton getinstance () {if(lazysingleton==NULL) return NewLazysingleton (); returnLazysingleton; }}2. A hungry man type single
When there are many variables that need to be shared, it takes too long to use a static variable for the entire life cycle of the class. The object is only present in the entire life cycle of the object. // a hungry man type class Single// class once loaded, the object already exists. {privatestaticnewprivatepublic static single getinstance () {return s;}}//Lazy TypeclassSingle2//class is loaded, there are no objects, and only objects are created when the GetInstance method is called. //defer
example instance = new Singleton () can be decomposed into the following pseudo-code:Memory = allocate (); 1: Allocating the object's memory space ctorinstance; 2: Initialize object instance = memory; 3: Set instance to point to the memory address just allocatedHowever, after reordering, the following are:Memory = allocate (); 1: Allocating the object's memory space instance = Memories; 3: Set
StaticMyObject getinstance () {if(MyObject = =NULL) {synchronized(MyObject.class) { if(MyObject = =NULL) {MyObject=NewMyObject (); } } } returnMyObject; }}Output:132019484913201948491320194849But is thread safety at this point?no~ but I can't give a counter-example push to, we have experience, please advise ~Fifth attempt:We also need to add the volatile keyword to the "sin
In program development, sometimes we only need an object, such as Log object, tool class, how to ensure that there is only one object in the entire application? This will use a singleton pattern, which can be seen by name, which guarantees that there is only one instance of the entire application. Singleton, single case, is a single instance. Now let's look at how to implement a
}sb.append ((char) ch) when it encounters a carriage return;} Here is the output of the string that guarantees that the last row does not have a carriage return (sb.length ()!=0) return sb.tostring (); return null;} Throw exception function public void Close () throws Ioexception{r.close ();}}Combine the above code with another class:Import Java.io.bufferedreader;import Java.io.filereader;import Java.io.ioexception;public class Testmybufferedreader { //main function public static void main
Type:Creation Design ModeIntent:Ensure that a class has only one instance and provides a global access point to it.Applicability: When a class can only have one instance and the customer can access it from a well-known access point When this unique instance should be extensible through subclass, and the customer should be able to use an extended instance without changing the code Structure:There are two common methods to implement the SingleTon
One: Lazy type1: Thread-safe double lock check mechanismpublic class singleton{Private Singleton () {}//privately constructed function, guaranteed not to be instantiated by the outside world (regardless of reflection)private static Singlecton single = NULL;public static Singlecton getinstance (){if (singleton = = null){Synchronized (Singleton.class){if (
() { if (instance==null) { synchronized (singleton.class) { if (null==instance) { instance=new Singleton ();}} } return instance;} }Static Inner classpublic class Singleton { private static class Lazyholder { private static final Singleton INSTANCE = new Singlet On ();
Sharing Mode = Singleton mode + factory mode + merging mode
Singleton mode:
Ensure that a class has only one instance and provides a global access point to it.
Structure:
Pay attention to the multi-threaded Singleton.
[Java]Package com. bankht. Flyweight. complex;/*** @ Author: special soldier-AK47* @ Creation Time: 0
initialized. Because the Singletonholder class is not actively used, the load Singletonholder class is displayed only if it is displayed by calling the GetInstance method, thus instantiating the instance. Imagine if instantiating instance is draining resources, I want him to delay loading, on the other hand, I don't want to instantiate when the singleton class loads, because I can't make sure that the Singleton
for the first time, It first reads singletonholder.instance, causing the Singletonholder class to be initialized, and when the class is loaded and initialized, it initializes its static domain to create an instance of singleton, because it is a static domain, so only when the virtual machine loads the class Initialized once and is guaranteed to be thread-safe by a virtual machine.The advantage of this pattern is that the GetInstance method is not syn
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.