threads – singleton

As from one of my previous posts about threading in csharp (c#), well with using Singletons you can use similar data between two different threads running.

In basics a singleton is when you want to have a single class instance so that you can share this class so that every time that you use it (even across threads) will only access the same class, it is very useful for when you have a printer spool so that you do not want to have x amount of printer spools (spool is when you have a list of print tasks waiting to print) and thus you only want to have one instance of a printer spool !!.

I have used the singleton creation from Microsoft website, that creates a singleton class that is thread safe which means that I am using the lock method that will lock on a object to stop thread contention and thus only creates a new instance of a Singleton class so that each thread will only get access to a single instance of that class.

So when you want to gain access to that single instance, you just call the

Singleton theSingleton = Singleton.Instance;

So here is the full source code, and below is the output where the output is displaying the value is incrementing otherwise if is was not a singleton class, the main class would print out 0-4 and also the runthismethod would output 0-9 instead!.

using System;
using System.Threading;
 
namespace monotestproject
{
	public sealed class Singleton
	{
		private static int _value;
		private static volatile Singleton instance;
		private static object syncRoot = new Object();
 
		public Singleton() { _value = 0;}
 
		public static Singleton Instance
		{
			get { 
				if (instance == null)
				{
					lock(syncRoot)
					{
						if (instance == null)
							instance = new Singleton();
					}
				}
				return instance;
			}
			private set {}
		}
 
		public int theValue 
		{
			get { return _value;}
			set { _value = value;}
		}
	}
 
	class MainClass
	{
		public static void Main (string[] args)
		{
			Singleton theSingleton = Singleton.Instance;
			// initialize the RunThisMethod as a thread
			Thread theThread = new Thread(RunThisMethod);
			theThread.Start();
 
			for (int j = 0; j < 5; j++)
			{
				theSingleton.theValue++;
				Console.WriteLine("Main Singleton value " + theSingleton.theValue);
				Thread.Sleep(100);
			}
		}
 
		// the method to create as a threadable method
		public static void RunThisMethod()
		{
			Singleton runsSingleton = Singleton.Instance;		
			for (int i =0; i < 10; i++)
			{
				runsSingleton.theValue++;
				Console.WriteLine("RunThisMethod Singleton value " + runsSingleton.theValue);
				Thread.Sleep(45);
			}
		}
	}
}

here is my output, as you can I am getting the singleton value incrementing, which is what should be happening.

Main Singleton value 1
RunThisMethod Singleton value 2
RunThisMethod Singleton value 3
RunThisMethod Singleton value 4
Main Singleton value 5
RunThisMethod Singleton value 6
RunThisMethod Singleton value 7
Main Singleton value 8
RunThisMethod Singleton value 9
RunThisMethod Singleton value 10
Main Singleton value 11
RunThisMethod Singleton value 12
RunThisMethod Singleton value 13
Main Singleton value 14
RunThisMethod Singleton value 15

Threading – running different parts of the program at “once”

A thread is when you “spawn” off another process (a process is like another program running within itself).

The operating system gives the impression that it is running allot of programs at the same time, but what is happening that allot of processes (programs) have access to the CPU for a limited amount of time, e.g. 10 milliseconds, and then leave the CPU execution stage whilst another process will enter there CPU execution stage (this is the part where the program is actually doing something).

To start with lets first create a method that is run within new thread, here we are just going to output a message and sleep for a bit, loop this over for 10 times.

// the method to create as a threadable method
public static void RunThisMethod()
{
	for (int i =0; i < 10; i++)
	{
		Console.WriteLine("RunThisMethod number : "+i.ToString() + " Sleep for 45");
		Thread.Sleep(45);
	}
}

Now lets create a thread, since a Thread has to be a delegated method but with .net 2 and above the .net environment will pick the correct delegate for you, so this means that you can just pass in the method name to the Thread class and that is it

	Thread theThread = new Thread(RunThisMethod);

To start the new Thread, it is as easy as start

	theThread.Start();

Here is the full source code, for demoing how the main and the runMyMethod flip between each other (e.g. each process has time in the processor(s) to run)

using System;
using System.Threading;
 
namespace myThread
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			// initialize the RunThisMethod as a thread
			Thread theThread = new Thread(RunThisMethod);
			theThread.Start();
 
			for (int j = 0; j < 5; j++)
			{
				Console.WriteLine("Main method number : "+j.ToString()+" Sleep for 100");
				Thread.Sleep(100);
			}
		}
 
		// the method to create as a threadable method
		public static void RunThisMethod()
		{
			for (int i =0; i < 10; i++)
			{
				Console.WriteLine("RunThisMethod number : "+i.ToString() + " Sleep for 45");
				Thread.Sleep(45);
			}
		}
	}
}

and here would be the output

Main method number : 0 Sleep for 100
RunThisMethod number : 0 Sleep for 45
RunThisMethod number : 1 Sleep for 45
RunThisMethod number : 2 Sleep for 45
Main method number : 1 Sleep for 100
RunThisMethod number : 3 Sleep for 45
RunThisMethod number : 4 Sleep for 45
Main method number : 2 Sleep for 100
RunThisMethod number : 5 Sleep for 45
RunThisMethod number : 6 Sleep for 45
Main method number : 3 Sleep for 100
RunThisMethod number : 7 Sleep for 45
RunThisMethod number : 8 Sleep for 45
Main method number : 4 Sleep for 100
RunThisMethod number : 9 Sleep for 45