According to Jon Skeet's article, the following pattern is bad as it is not thread safe.
// Bad code! Do not use!
public sealed class Singleton
{
private static Singleton instance = null;
private Singleton()
{
}
public static Singleton Instance
{
get
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
I have not learnt threading yet so it is a bit abstract to me. Could you give me a simple code to simulate the threading problem (we get notified when the problem occurs)?
Well thats pretty simple, just let something access a property within your singleton in parallel, for example like this console app.
class Program
{
static void Main(string[] args)
{
var threads = Enumerable.Repeat(new Action(() => Console.WriteLine(Singleton.Instance.guid)), 10);
Parallel.ForEach(threads, t => t());
Console.Read();
}
}
(I've added a guid property to your class to test that)
public sealed class Singleton
{
public Guid guid = Guid.NewGuid();
private static Singleton instance = null;
private Singleton()
{
}
public static Singleton Instance
{
get
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
}
The issue with this singleton implementation is that 2 threads can access the getter simultaneously and each one will create a new instance. So the first thread might end up with a different instance than the second one... which can lead to unexpected behavior.
This is just in reply to OP comment:
static void Main(string[] args)
{
int test = 5;
Task<Singleton>[] arr =
{
Task<Singleton>.Factory.StartNew(() => Singleton.Instance),
Task<Singleton>.Factory.StartNew(() => Singleton.Instance),
};
Task.WaitAll(arr);
foreach (var item in arr)
{
Singleton s = item.Result;
s.MyProperty = test++;
Console.WriteLine(s.MyProperty);
}
}
MyProperty is just an int property i added.
Related
I wrote static singleton and add class inside the Singleton class.
However, it seem like it broke the pattern.
any advice why?
public sealed class Singleton
{
private static readonly Singleton instance = new Singleton();
public int MyProperty { get; set; } = 10;
static Singleton() { }
private Singleton() { }
public static Singleton Instance {get { return instance; } }
public class SecondSingleton
{
public Singleton secondInstance;
public SecondSingleton()
{
secondInstance = new Singleton();
secondInstance.MyProperty = 20;
}
}
}
class Program
{
static void Main(string[] args)
{
Singleton s1 = Singleton.Instance;
Singleton.SecondSingleton s2 = new Singleton.SecondSingleton();
Console.WriteLine($"s1.MyProperty = {s1.MyProperty}");
Console.WriteLine($"s2.MyProperty = {s2.secondInstance.MyProperty}");
Console.ReadLine();
}
}
Asper WIKI : Singleton restricts the instantiation of a class to one "single" instance. This is useful when exactly one object is needed to coordinate actions across the system.
The moment you say "SecondInstance", then it is broke the pattern.
You are creating new Singleton in SecondSingleton constructor, which means secondInstance != Singleton.Instance. Instead, you should get the Instance, i.e: secondInstance = Singleton.Instance;
I have a bit of weird problem that is hard to explain. I have singleton class where in the constructor I have to run a task to initialize some components/resources.
I used 2 implementation of singleton from C# in Depth and in one case everything is working fine, in another case - not.
Code is available below with some comments.
The main problem that for some reason task is not started in one case, when I am using static field with initialier and static contructor (class Test2).
I made some other tests and looks like with the implementation 2 task does not start asynchronically, but starts synchronically after waiting time.
Implementation one. everything is working as expected
public sealed class Test1
{
private static Test1 instance = null;
private static readonly object padlock = new object();
private Test1()
{
using (AutoResetEvent startEvent = new AutoResetEvent(false))
{
new Task(() => TaskThread(startEvent)).Start();
if (!startEvent.WaitOne(1000))
{
throw new Exception("ERROR");
}
}
}
public int Result()
{
return 10;
}
private void TaskThread(AutoResetEvent startEvent)
{
//I am initializing some stuff here
startEvent.Set();
}
public static Test1 Instance
{
get
{
lock (padlock)
{
if (instance == null)
{
instance = new Test1();
}
return instance;
}
}
}
}
Implementation 2, task is never started, or started after waiting time of an event
public sealed class Test2
{
private static readonly Test2 instance = new Test2();
static Test2()
{
}
private Test2()
{
using (AutoResetEvent startEvent = new AutoResetEvent(false))
{
new Task(() => TaskThread(startEvent)).Start();
//here it fails to wait successfully and throws an
//exception. Time limit is not reached
if (!startEvent.WaitOne(1000))
{
throw new Exception("ERROR");
}
}
}
public int Result()
{
return 20;
}
private void TaskThread(AutoResetEvent startEvent)
{
//I am initializing some stuff here as well
//but in this implementation code is never reached
startEvent.Set();
}
public static Test2 Instance
{
get
{
return instance;
}
}
}
I am curious why is this happening and how to avoid this problems in future. Thanks a lot!
The root cause of such 'strange' behavior is pretty simple - CLR executes static constructor under a lock. That prevents created thread from entering TaskThread() method and setting startEvent to signaled state.
After you face with such a problem and puzzle for several hours why this is happening, you start to understand why many sources advise not to use doubtful constructs like static constructors, global variables, etc.
I have a class which implements the Singleton design pattern. However, whenever i try to get an instance of that class, using Activator.CreateInstance(MySingletonType) only the private constructor is called. Is there any way to invoke other method than the private constructor?
My class is defined as follow:
public class MySingletonClass{
private static volatile MySingletonClassinstance;
private static object syncRoot = new object();
private MySingletonClass()
{
//activator.createInstance() comes here each intantiation.
}
public static MySingletonClassInstance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
instance = new MySingletonClass();
}
}
return instance;
}
}
}
And the instantiation as follow:
Type assemblyType = Type.GetType(realType + ", " + assemblyName);
IService service = Activator.CreateInstance(assemblyType, true) as IService;
Activator.CreateInstance, except for one extreme edge-case, always creates a new instance. I suggest that you probably dont want to use Activator here.
However, if you have no choice, the hacky hack hack hack is to make a class that inherits from ContextBoundObject, and decorate it with a custom subclass of ProxyAttribute. In the custom ProxyAttribute subclass, override CreateInstance to do whatever you want. This is all kinds of evil. But it even works with new Foo().
Hei i do not know why are you creating an object of singleton class using reflection.
the basic purpose of singleton class is that it has only one object and has global access.
however you can access any of your method in singleton class like :
public class MySingletonClass {
private static volatile MySingletonClass instance;
private static object syncRoot = new object();
private MySingletonClass() { }
public static MySingletonClass MySingletonClassInstance {
get {
if (instance == null) {
lock (syncRoot) {
if (instance == null)
instance = new MySingletonClass();
}
}
return instance;
}
}
public void CallMySingleTonClassMethod() { }
}
public class program {
static void Main() {
//calling a
methodMySingletonClass.MySingletonClassInstance
.CallMySingleTonClassMethod();
}
}
I am struggling with using Singleton design pattern. I am trying to use it in this simple console application. I have a problem with it in the Main method in Program class. I want to define object from the Singleton class such as: var data = Singleton.Instance; but I don't know why I can't do that
Also, I don't know why I am getting the following error message when I run the program:
Unhandled Exception: System.NullRefernceException: Object reference not
set to an instance of an object.
So how to fix that?
Singleton Class:
namespace Singleton
{
class Singleton
{
//Variable
private static Singleton instance;
private List<string> Messages;
//Constructor
private Singleton() { }
//Property
public static Singleton Instance
{
get
{
if (instance == null)
{
instance = new Singleton();
}
return instance;
}
}
//Methods
public void Message(string message)
{
Messages.Add(message);
}
public bool HasMessage(string message)
{
return Messages.Contains(message);
}
}
}
Program Class:
namespace Singleton
{
class Program
{
static void Main(string[] args)
{
var data = Singleton.Instance;
Singleton.Instance.Message("Hello World!!!");
if(Singleton.Instance.HasMessage("12"))
Console.WriteLine("NO STRING!!!");
else
Console.WriteLine("There is a match");
}
}
}
UPDATE:
Guys, I really appreciate your help so far. The program is working now but the logic is not working. If you look at the main program, you will see that the list only has "Hello World!!!". However, when I used the HasMessage method doesn't work. Because the program keeps showing "There is a match". But it should show me "NO STRING!!!" as there is no match. So how to fix that?
Your field Messages is not initialized to anything. That is why you are getting the exception. In your class do:
private List<string> Messages = new List<string>();
You may also look at Thread Safe Singleton implementation by Jon Skeet
EDIT:
Based on the updated question. Your Check and Message are opposite. It should be:
if (Singleton.Instance.HasMessage("12"))
Console.WriteLine("There is a match");
else
Console.WriteLine("NO STRING!!!");
Your method HasMessage returns true if the passed parameter is present in the list and false otherwise.
It looks like you're almost there. Consider rewriting your code like this:
class Singleton
{
//Variable
private static Singleton Instance;
private List<string> Messages;
//Constructor
private Singleton()
{
Messages = new List<string>(); //Make sure to instantiate instance types before use.
}
//Property
public static Singleton GetInstance()
{
if (Instance == null)
{
Instance = new Singleton();
}
return Instance;
}
//Methods
public void Message(string message)
{
Messages.Add(message);
}
public bool HasMessage(string message)
{
return Messages.Contains(message);
}
}
There are some helpful C# tutorials for design patterns on this site.
However, when I used the HasMessage method doesn't work. Because the program keeps showing "There is a match". But it should show me "NO STRING!!!" as there is no match. So how to fix that?
This should really be a separate question, but I'll answer it anyway. You've got your condition backwards. Your code is says to write "no string" if the instance does contain the message "12", and "There is a match" if it does not. Try this:
if(Singleton.Instance.HasMessage("12"))
Console.WriteLine("There is a match");
else
Console.WriteLine("NO STRING!!!");
private List<String> Messages;
Here is your problem. The member has never been instanciated in your code. You could do as follows:
//Constructor
private Singleton()
{
Messages = new List<string>();
}
Also i suggest you to use proper naming conventions for your local variables and members. Change instance to m_Instance and Messages to m_Messages. Also try to implement singletons in a thread-safe way... for more informations look at this page.
That's the implementation of Singleton:
public sealed class SingletonExample
{
//static Field
private static SingletonExample _seInstance = null;
private int _nCounter = 0;
// private constructor
private SingletonExample() { _nCounter = 1; }
//public static get(), with creating only one instance EVER
public static SingletonExample SeInstance
{
get { return _seInstance ?? (_seInstance = new SingletonExample()); }
}
}
How to invoke and create an instance?
SingletonExample si1 = SingletonExample.SeInstance;
SingletonExample si2 = SingletonExample.SeInstance; // it will be the same object
System.Diagnostics.Debug.WriteLine(si1.Equals(si2));// TRUE
I have a bit of code that I've been trying to examine for thread safety. I'm using the basic lazy singleton model found here. I was wondering if it is still thread safe if I'm putting the instance in the HttpApplicationState object. I need to access this instance across all instances of the web application, so if this is not thread safe how can I make it thread safe?
public sealed class EmailWorker {
private HttpApplicationState _app;
private const EMAIL_WORKER = "EmailWorker";
EmailWorker() { }
class NestedWorker {
static NestedWorker() { }
internal static readonly EmailWorker Instance = new EmailWorker();
}
public static void Initialize(HttpApplicationState appState) {
_appState = appState;
_appState.Lock();
if (_appState[EMAIL_WORKER] == null) {
_appState.Add(EMAIL_WORKER, NestedWorker.Instance);
}
_appState.UnLock();
}
public static EmailWorker Instance {
get {
// TODO: If we haven't called Initialize() first then throw exception
return (EmailWorker)_appState[EMAIL_WORKER];
}
}
}
You don't need to use Application state at all.
It should be thread-safe, but why bother?
A "standard" singleton will also be accessible across the entire application, and it won't require injecting and keeping a reference to the HttpApplicationState:
public sealed class EmailWorker
{
private EmailWorker() { }
private static class NestedWorker
{
static NestedWorker() { }
internal static readonly EmailWorker Instance = new EmailWorker();
}
public static EmailWorker Instance
{
get { return NestedWorker.Instance; }
}
}