Concurrency Control With Locks Expanded C# with Example



Concurrency Control With Locks Expanded C# with Example

 using System;
using System.Threading;

namespace CSharpMultiThreading
{
	class Program
	{
		static readonly object _object = new object();

		static void CommonMethod()
		{
			Monitor.Enter(_object);
			try
			{
				Thread.Sleep(50);
				Console.WriteLine("CommonMethod executing for Secondary Thread#{0} on TickCount: {1}", Thread.CurrentThread.ManagedThreadId, Environment.TickCount);
			}
			finally
			{
				Monitor.Exit(_object);
			}
		}

		static void Main()
		{
			// Create and Start ten new threads.
			for (int i = 0; i < 10; i++)
			{
				new Thread(CommonMethod).Start();
			}
		}
	}
} 

0 Comment's

Comment Form

Submit Comment