Concurrency Control With Locks- Synchronization C# with Example



Concurrency Control With Locks- Synchronization C# with Example

 using System;
using System.Threading;

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

		static void CommonMethod()
		{
			// Lock on the readonly object.
			// ... Inside the lock, sleep for 100 milliseconds.
			lock (_object)
			{
				Thread.Sleep(50);
				Console.WriteLine("CommonMethod executing for Secondary Thread#{0} on TickCount: {1}", Thread.CurrentThread.ManagedThreadId, Environment.TickCount);
			}
		}

		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