You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Text;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace Singleton.ThreadSageSingleton
|
|
|
|
|
{
|
|
|
|
|
internal class Client
|
|
|
|
|
{
|
|
|
|
|
public static void DoTest()
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine(
|
|
|
|
|
"Thread safe Singleton",
|
|
|
|
|
"If you see the same value, then both instances are same.",
|
|
|
|
|
"If you see differnt values, then 2 differnect instances are created.",
|
|
|
|
|
"RESULT: ");
|
|
|
|
|
|
|
|
|
|
Thread process1 = new Thread(() =>
|
|
|
|
|
{
|
|
|
|
|
TestSingleton("FOO");
|
|
|
|
|
});
|
|
|
|
|
Thread process2 = new Thread(() =>
|
|
|
|
|
{
|
|
|
|
|
TestSingleton("BAR");
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
process1.Start();
|
|
|
|
|
process2.Start();
|
|
|
|
|
|
|
|
|
|
process1.Join();
|
|
|
|
|
process2.Join();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static void TestSingleton(string value)
|
|
|
|
|
{
|
|
|
|
|
Singleton singleton = Singleton.GetInstance(value);
|
|
|
|
|
Console.WriteLine(singleton.Value);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|