using System.Collections.Generic;
using System.Threading.Tasks;
public static class Program
public static void Main()
List<Task> tasks = new List<Task>();
for (int index = 0; index < 10; index++)
tasks.Add(Task.Factory.StartNew(() => Console.WriteLine(TestObject.Instance.GetMessage())));
Task.WaitAll(tasks.ToArray());
Console.WriteLine("Completed...");
private static TestObject pInstance;
private readonly static Mutex pInstanceMutex = new Mutex(false);
private readonly object pMsgLock = new Object();
private readonly Random pRandom;
public static TestObject Instance
pInstanceMutex.WaitOne();
pInstance ??= new TestObject();
Thread.Sleep(pInstance.GetRandomDelay());
pInstanceMutex.ReleaseMutex();
public string GetMessage()
string Result = "Message from Thread: " + Environment.CurrentManagedThreadId;
private TimeSpan GetRandomDelay()
int randomValue = pRandom.Next(50,300);
TimeSpan Result = TimeSpan.FromMilliseconds(randomValue);
Console.WriteLine("TestObject created by Thread: " + Environment.CurrentManagedThreadId);