C # base: Example 3 - thread synchronization thread three methods

A method for implementing thread synchronization

1.1 Use the lock keyword implement thread synchronization

lock keyword can be used to ensure complete block of code to run, the other thread is not terminated. It is mainly achieved by a mutex to obtain a given object code block during operation.
If you use the lock keyword in a static method, you can not use this.

using System;
using System.Threading;

namespace ConsoleApp1
{
    class Program
    {
        private int num = 20;  // 设置当前总票数
        private void Ticket()
        {
            while (true)
            {
                lock (this)
                {
                    if (num > 0)
                    {
                        Thread.Sleep(50);
                        Console.WriteLine(Thread.CurrentThread.Name + "-----票数" + num--);
                    }
                }
            }

        }
        static void Main(string[] args)
        {
            Program p = new Program();
            Thread t1 = new Thread(new ThreadStart(p.Ticket));
            t1.Name = "线程1";

            Thread t2 = new Thread(new ThreadStart(p.Ticket));
            t2.Name = "线程2";

            Thread t3 = new Thread(new ThreadStart(p.Ticket));
            t3.Name = "线程3";

            Thread t4 = new Thread(new ThreadStart(p.Ticket));
            t4.Name = "线程4";

            t1.Start();
            t2.Start();
            t3.Start();
            t4.Start();
        }
    }
}

1.2 Monitor class implements Thread Synchronization

Monitor class provides a synchronization mechanism to access the object, it is an object controlled by granting access to a single thread to lock an object, the object lock provides the ability to limit access to the code block. When a thread owns an object lock, no other thread can acquire the lock.

using System;
using System.Threading;

namespace ConsoleApp1
{
    class Program
    {
        public static Program p = new Program();
        private static int num = 30;  // 设置当前总票数
        private static void Ticket()
        {
            while (true)
            {
                Monitor.Enter(p);
                {
                    if (num > 0)
                    {
                        Thread.Sleep(100);
                        Console.WriteLine(Thread.CurrentThread.Name + "-----票数" + num--);
                    }
                }
                Monitor.Exit(p);
            }

        }
        static void Main(string[] args)
        {
  
            Thread t1 = new Thread(new ThreadStart(Program.Ticket));
            t1.Name = "线程1";

            Thread t2 = new Thread(new ThreadStart(Program.Ticket));
            t2.Name = "线程2";

            Thread t3 = new Thread(new ThreadStart(Program.Ticket));
            t3.Name = "线程3";

            Thread t4 = new Thread(new ThreadStart(Program.Ticket));
            t4.Name = "线程4";

            t1.Start();
            t2.Start();
            t3.Start();
            t4.Start();
        }
    }
}

1.3 Use Mutex class implements Thread Synchronization

Implement thread synchronization can also use the Mutex class, which is the synchronization primitives, it only granted exclusive access to the shared resource to a thread. Monitor and Mutex essentially similar, the difference is with the monitor, Mutex class can make cross-process thread synchronization.

using System;
using System.Threading;

namespace ConsoleApp1
{
    class Program
    {
        private int num = 20;  // 设置当前总票数
        private void Ticket()
        {
            while (true)
            {
                Mutex mutex = new Mutex(true);
                mutex.WaitOne();

                if (num > 0)
                {
                    Thread.Sleep(50);
                    Console.WriteLine(Thread.CurrentThread.Name + "-----票数" + num--);
                }
                
                mutex.ReleaseMutex();
            }

        }
        static void Main(string[] args)
        {
            Program p = new Program();
            Thread t1 = new Thread(new ThreadStart(p.Ticket));
            t1.Name = "线程1";

            Thread t2 = new Thread(new ThreadStart(p.Ticket));
            t2.Name = "线程2";

            Thread t3 = new Thread(new ThreadStart(p.Ticket));
            t3.Name = "线程3";

            Thread t4 = new Thread(new ThreadStart(p.Ticket));
            t4.Name = "线程4";

            t1.Start();
            t2.Start();
            t3.Start();
            t4.Start();
        }
    }
}
Published 38 original articles · won praise 29 · views 50000 +

Guess you like

Origin blog.csdn.net/ruotianxia/article/details/105169825