Sunday, 3 December 2017

C# Program to Demonstrate Lock in Thread


Code:

using System;
using System.Threading;

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

    static void TEST()
    {
        lock (_object)
        {
            Thread.Sleep(100);
            Console.WriteLine(Environment.TickCount);
        }
    }
    static void Main()
    {
        for (int i = 0; i < 10; i++)
        {
            ThreadStart start = new ThreadStart(TEST);
            new Thread(start).Start();
        }
    }
}


Output:

900500
900593
900687
900796
900890
900999
901092
901186
901295
901389


More C# Programs:




















100+ Best Home Decoration Ideas For Christmas Day 2019 To Make Home Beautiful

Best gifts for Christmas Day | Greeting cards for Christmas Day | Gift your children a new gift on Christmas day This Christmas d...