java singleton example

Alibabacloud.com offers a wide variety of articles about java singleton example, easily find your java singleton example information here online.

Java Design Pattern Analysis-singleton

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

Java design Pattern--Singleton mode

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

JAVA design Pattern-Singleton mode

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

Java Singleton Mode

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

Java design pattern----Singleton mode

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

java-Java Connection pool for singleton mode

1 PackageCom.dawning.gridview.app.resourcemanagement.service.servicemanagement.discoverresourceutil;2 3 ImportJava.util.concurrent.ExecutorService;4 Importjava.util.concurrent.Executors;5 6 /**7 * Thread pool single case8 * @class Singletontheadpool9 * @date 2015-7-7 pm 3:29:04Ten * @authorWANGHC One */ A Public classSingletontheadpool { - /** - * Single Case the */ - Private Static classsingletonholder{ - Private Static FinalExecutorservice Mypool = Executors.newfixedth

Problems with Java Singleton mode attention

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

Java Singleton mode

Import Java.util.concurrent.locks.Lock;Import Java.util.concurrent.locks.ReentrantLock;public class Singleton4 {Private Singleton4 () {};private static Singleton4 single = NULL;private static lock lock = new Reentrantlock ();public static Singleton4 getinstance () {if (single = null) {Getsingle ();}return single;}private static void Getsingle () {Bondage 1Lock.lock ();if (single = null) {Single = new Singleton4 ();}Lock.unlock ();Bondage 2 synchronized (singleton4.class)//{ if (single = null)

Java Singleton mode

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

Java Singleton mode &static member variable differences

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

Java Common design pattern-Singleton mode

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

Java-Singleton mode and multithreading

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

Java design pattern------Singleton mode

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

Java Design Patterns (1)-------Singleton, factory, value object, decoration mode

}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

Java design pattern to SingleTon)

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

Design mode-Singleton mode (Java)

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 (

Java design pattern----Singleton mode

() { 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 ();

JAVA and mode 12th-yuan mode = Singleton mode + factory mode + Synthesis Mode

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

Java design pattern-Singleton mode

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

Java Singleton mode

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

Total Pages: 15 1 .... 11 12 13 14 15 Go to: Go

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.