原文出自:http://www.javaworld.com/community/node/7692
There are two ways singletons are created using the Double Check Locking and the Holder Idiom for lazy loading. Both of them create a static singleton object which does not help garbage collection much.
In search of a new way to create singleton without using static object came across this solution which gets rid of creating object in static way and also helps garbage collection as we wrap singleton object with Soft Reference.
package com.singleton;
import java.lang.ref.SoftReference;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Semaphore;
/**
* SoftSingleton Idiom It does not create a static object and also creates a
* soft reference and thus in turn helps garbage collection.
*
* @author ajitkoti
*
*/
public class SingletonTest {
// This List would always contain only 1 Singleton object
private static List<SoftReference<SingletonTest>> singletonList = new ArrayList<SoftReference<SingletonTest>>();
// very imp to keep the permit as 1 if not other threads would acquire and
// create the Singleton
private static Semaphore semaphore = new Semaphore(1);
private SingletonTest() {
System.out.println("Hi I am a Singleton Just born");
}
/**
* Returns an instance of SingletonTest
*
* @return
*/
public static SingletonTest getInstance() {
if (singletonList.isEmpty() || singletonList.get(0).get() == null) {
try {
semaphore.acquire();
// Double check so that if the second thread acquires the permit
// ,
// while first Thread has all ready created the Singleton
if (singletonList.isEmpty()
|| singletonList.get(0).get() == null) {
// Only 1 Thread will ever enter here and create the
// Singleton
singletonList.add(new SoftReference<SingletonTest>(
new SingletonTest()));
}
semaphore.release();
} catch (InterruptedException e2) {
System.err.println("Could not create Singleton - "
+ e2.getMessage());
Thread.currentThread().interrupt(); // restore the interrupt
}
}
return singletonList.get(0).get();
}
/**
* Main method to test the Singleton creation
*
* @param args
*/
public static void main(String[] args) {
int noOfThreads = 1000;
ExecutorService executor = Executors.newFixedThreadPool(noOfThreads);
for (int i = 0; i < noOfThreads; i++) {
executor.execute(new Runnable() {
public void run() {
System.out.println("Calling and Creating singletonTest"
+ SingletonTest.getInstance());
}
});
}
executor.shutdown();
}
}
分享到:
相关推荐
此示例展示了Qml 的单例模式(类似全局对象,只生成一次实例,可全局使用) surfsky.cnblogs.com
设计模式C++学习之单例模式(Singleton)
java Singleton单例模式 java Singleton单例模式
单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一。这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式。 这种模式涉及到一个单一的类,该类负责创建自己的对象,同时确保只有单个...
在Java应用中,单例对象能保证在一个...3、有些像交易所的核心交易引擎,控制着交易流程,如果该类可以创建多个的话,系统完全乱了,只有使用单例模式,才能保证核心交易服务器独立控制整个流程。 CSDN代码的详细解释。
简单的单例模式举例Singleton 分为恶汉式 懒汉式
设计模式总结-模板设计模式,单例模式(singleTon)
一个产生随机数的例子,整个应用程序中只需要一个类的实例来产生随机数,客户端程序从类中获取这个实例,调用这个实例的方法nextInt(),公用的方法访问需要进行同步,这是单例模式需要解决的同步问题。
单例模式 Singleton 单例模式线程安全问题和拓展
4、单例模式(Singleton Pattern) 用意:仅允许生成一个对象时
C++11实现线程安全的单例代码和测试代码,包含singleton.h,main.cpp,希望帮助到大家。
单例模式的特点有三: 单例类只能有一个实例。 单例类必须自己创建自己的唯一实例。 单例类必须给所有其他对象提供这一实例。 Singleton模式包含的角色只有一个,就是Singleton。Singleton拥有一个私有构造函数,...
单例模式(Singleton)的6种实现
Singleton pattern单例模式应用
singleton单例模式1
php /** * 单例模式 * * 保证一个类仅有一个实例,并提供一个访问它的全局访问点 * */ class Singleton { static private $_instance = null; private function __construct() { } static public function ...
深入浅出Singleton,详细掌握单例模式
单例模式,您能写出几个呢?这个PPT将为你全面阐述5种不同形式的单例模式。
单例模式(singleton)顾名思义,就是只有一个实例。作为对象的创建模[GOF95], 单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例。这个类称为单例类。