singleton

ScheduledExecutorService - Check if scheduled task has already been completed

心已入冬 提交于 2020-01-09 11:19:08
问题 I have a server side application where clients can request to reload the configuration. If a client request to reload the configuration, this should not be done immediately, but with an delay of 1 minute. If another client also requests to reload the configuration in the same minute, this request should be ignored. My idea is to schedule a task with a ScheduledExecutorService like: ScheduledExecutorService service = Executors.newSingleThreadScheduledExecutor(); service.schedule(new

What is the point of making the singleton instance volatile while using double lock? [duplicate]

寵の児 提交于 2020-01-09 06:01:08
问题 This question already has answers here : Why is volatile used in double checked locking (6 answers) Closed 2 years ago . private volatile static Singleton uniqueInstance In a singleton when using double lock method for synchronization why is the single instance declared as volatile ? Can I achieve the same functionality without declaring it as volatile ? 回答1: Without volatile the code doesn't work correctly with multiple threads. From Wikipedia's Double-checked locking: As of J2SE 5.0, this

Is using a singleton for the connection a good idea in ASP.NET website

独自空忆成欢 提交于 2020-01-09 05:27:06
问题 I'm currently using a singleton on my web application so that there is always only one connection to the database. I want to know if it's a good idea because right now I'm having trouble with that error: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached. Another important point is that my website is currently in dev and not a lot of people go on it so I don

Is using a singleton for the connection a good idea in ASP.NET website

不打扰是莪最后的温柔 提交于 2020-01-09 05:27:04
问题 I'm currently using a singleton on my web application so that there is always only one connection to the database. I want to know if it's a good idea because right now I'm having trouble with that error: Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached. Another important point is that my website is currently in dev and not a lot of people go on it so I don

Singleton in Android

久未见 提交于 2020-01-08 16:01:52
问题 I have followed this link and successfully made singleton class in Android. http://www.devahead.com/blog/2011/06/extending-the-android-application-class-and-dealing-with-singleton/ Problem is that i want a single object. like i have Activity A and Activity B. In Activity A I access the object from Singleton class . I use the object and made some changes to it. When I move to Activity B and access the object from Singleton Class it gave me the initialized object and does not keep the changes

What's Alternative to Singleton

旧街凉风 提交于 2020-01-08 11:23:19
问题 We have a class that holds configuration information for the application. It used to be a singleton. After some architectural review, we were told to remove the singleton. We did see some benefits of not using singleton in the unit testing because we can test different configurations all at once. Without singleton, we have to pass the instance around everywhere in our code. It's getting so messy so we wrote a singleton wrapper. Now we are porting the same code to PHP and .NET, I am wondering

What's Alternative to Singleton

最后都变了- 提交于 2020-01-08 11:22:08
问题 We have a class that holds configuration information for the application. It used to be a singleton. After some architectural review, we were told to remove the singleton. We did see some benefits of not using singleton in the unit testing because we can test different configurations all at once. Without singleton, we have to pass the instance around everywhere in our code. It's getting so messy so we wrote a singleton wrapper. Now we are porting the same code to PHP and .NET, I am wondering

Javascript and singleton pattern

为君一笑 提交于 2020-01-07 09:47:15
问题 I am reading the book by Addy Osmani book, Learning Javascript design patterns. http://addyosmani.com/resources/essentialjsdesignpatterns/book/ I have created a file called singleton.js it contains: var mySingleton = (function() { var instance; function init() { var privateRandomNumber = Math.random(); return { getRandomNumber : function() { return privateRandomNumber; } }; return { getInstance : function() { if (!instance) { instance = init(); } return instance; } }; })(); I have a file that

c++ Meyers singleton undefined reference

只愿长相守 提交于 2020-01-07 04:38:07
问题 I have the following code that implements a basic Meyers singletone: #ifndef _cConfigFile_HH #define _cConfigFile_HH class cConfigFile { public: static cConfigFile& getInstance() { static cConfigFile instance; return instance; }; private: cConfigFile(); }; #endif My compiler doesn't allow me to compile this, giving the following error: /include/cConfigFile.hh:7: undefined reference to `cConfigFile::cConfigFile()' From the error I understand that I need to declare "instance" in a .cpp file,

Definite class name in a singleton method

烈酒焚心 提交于 2020-01-07 03:10:21
问题 Here is a sample code of a singleton method: + (id)sharedManager { static MyManager *sharedMyManager = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ sharedMyManager = [[self alloc] init]; }); return sharedMyManager; } Is there a way to get rid of the certain class name declaration MyManager * and replace it with some abstract type like self * or instancetype ? 回答1: You can use id within the method, to avoid referencing the actual class name, and then specify an