zoukankan      html  css  js  c++  java
  • C#各种同步方法 lock, Monitor,Mutex, Semaphore, Interlocked, ReaderWriterLock,AutoResetEvent, ManualResetEvent

    看下组织结构:

    System.Object
      System.MarshalByRefObject
        System.Threading.WaitHandle
          System.Threading.Mutex
          System.Threading.Semaphore
          System.Threading.EventWaitHandle
            System.Threading.ManualResetEvent

            System.Threading.AutoResetEvent

    System.Object

      System.Threading.Interlocked
      System.Threading.Monitor

      System.Threading.ReaderWriterLock

     

    1, lock 关键字其实就是对 Monitor 类的 Enter()和 Exit()方法的封装。通过 try......catch......finally 语句块确保在 lock 语句块结束后执行 Monitor.Exit()方法,释放互斥锁。下面2段代码等价:

         

    lock(locker)
    {
      //do something
    }
    View Code
    Monitor.Enter(locker);
    try
    {
        // do something
    }
    finally
    {
        Monitor.Exit(locker);
    }
    View Code

    2,

    Monitor类通过向单个线程授予对象锁来控制对对象的访问。对象锁提供限制访问临界区的能力。当一个线程拥有对象的锁时,其他任何线程都不能获取该锁。还可以使用 Monitor 来确保不会允许其他任何线程访问正在由锁的所有者执行的应用程序代码节,除非另一个线程正在使用其他的锁定对象执行该代码。通过对 lock 关键字的分析我们知道,lock 就是对 Monitor 的 Enter 和 Exit 的一个封装,而且使用起来更简洁,因此 Monitor 类的 Enter()和 Exit()方法的组合使用可以用 lock 关键字替代。 
       Monitor 类的常用方法: 
            TryEnter(): 
                能够有效的解决长期死等的问题,如果在一个并发经常发生,而且持续时间长的环境中使用 TryEnter,可以有效防止死锁或者长时间的等待。比如我们可以设置一个等待时间 bool gotLock = Monitor.TryEnter(myobject,1000),让当前线程在等待 1000 秒后根据返回的 bool 值来决定是否继续下面的操作。

            Wait() :

                释放对象上的锁以便允许其他线程锁定和访问该对象。在其他线程访问对象时,调用线程将等待。脉冲信号用于通知等待线程有关对象状态的更改。 
            Pulse(): 
            PulseAll(): 
                向一个或多个等待线程发送信号。该信号通知等待线程锁定对象的状态已更改,并且锁的所有者准备释放该锁。等待线程被放置在对象的就绪队列中以便它可以最后接收对象锁。一旦线程拥有了锁,它就可以检查对象的新状态以查看是否达到所需状态。注意:Pulse、PulseAll 和 Wait 方法必须从同步的代码块内调用。

           static object locker = new object();
            static bool isHave = false;
    
            static void Produce()
            {
                lock (locker)
                {
                    while (true)
                    {
                        //如果已有产品,则等待消费完成
                        if (isHave)
                            Monitor.Wait(locker);
                        Console.WriteLine("生产一个");
                        Thread.Sleep(1000);
                        isHave = true;
                        Monitor.Pulse(locker);
                    }
                }
            }
            static void Consume()
            {
                lock (locker)
                {
                    while (true)
                    {
                        //如果没有产品,则等待生产完成
                        if (!isHave)
                            Monitor.Wait(locker);
                        Console.WriteLine("消费一个");
                        Thread.Sleep(500);
                        isHave = false;
                        Monitor.Pulse(locker);
                    }
                }
            }
    View Code

    在main函数中调用:

                new Thread(Produce).Start();
                new Thread(Consume).Start();
    View Code

    3, Mutex互斥体

    public class Test
        {
            // Create a new Mutex. The creating thread does not own the
            // Mutex.
            private static Mutex mut = new Mutex();
    
            public static void MyThreadProc()
            {
                for (int i = 0; i < 2; i++)
                {
                    UseResource();
                }
            }
    
            // This method represents a resource that must be synchronized
            // so that only one thread at a time can enter.
            private static void UseResource()
            {
                // Wait until it is safe to enter.
                mut.WaitOne();
    
                Console.WriteLine("{0} has entered the protected area",
                    Thread.CurrentThread.Name);
    
                // Place code to access non-reentrant resources here.
    
                // Simulate some work.
                Thread.Sleep(500);
    
                Console.WriteLine("{0} is leaving the protected area
    ",
                    Thread.CurrentThread.Name);
    
                // Release the Mutex.
                mut.ReleaseMutex();
            }
        }
    View Code
    Test test = new Test();
                for (int i = 0; i < 3; i++)
                {
                    Thread myThread = new Thread(new ThreadStart(Test.MyThreadProc));
                    myThread.Name = String.Format("Thread{0}", i + 1);
                    myThread.Start();
                }
    View Code

     mutex还可以判断系统是否已经有一个进程存在。

    4,Semaphore 信号量

    static Semaphore sph = new Semaphore(0, 3);
            static void TProc()
            {
                while (true)
                {
                    if (sph.WaitOne(500, false))
                    {
                        try
                        {
                            Console.WriteLine("thread" + Thread.CurrentThread.Name + ":enter");
                            Thread.Sleep(1000);
                        }
                        finally
                        {
                            sph.Release();
                            Console.WriteLine("thread" + Thread.CurrentThread.Name + ":exit");
                        }
                    }
                    else
                    {
                        Console.WriteLine("thread" + Thread.CurrentThread.Name + ":time out");
                    }
                }
            }
    View Code
    Thread t = null;
                for (int i = 0; i < 2; i++)
                {
                    t = new Thread(TProc);
                    t.Name = i.ToString();
                    t.Start();
                }
                Console.WriteLine("main sleep 4s");
                Thread.Sleep(4000);
                sph.Release(2);
    View Code

    5,Interlocker类为多个线程共享的变量提供原子操作,它是一个静态类,主要的成员方法如下:
    Add:以原子操作的形式,添加两个整数并用两者的和替换第一个整数。
    Exchange:以原子操作的形式将变量设置为指定的值,并返回先前值
    CompareExchange:比较两个值是否相等,如果相等,则替换其中一个值
    Equals:确定两个Object 实例是否相等
    Increment:以原子操作的形式递增指定变量的值并存储结果
    Decrement:以原子操作的形式递减指定变量的值并存储结果
    Read:返回一个以原子操作形式加载的 64 位值

    Interlocked.CompareExchange(ref obj, new object(), null);
    View Code

    6, ReaderWriterLock

    static ReaderWriterLock rwLock = new ReaderWriterLock();
    static object locker = new object();
    static void Main(string[] args)
    {
        Thread t = null;
        for(int i = 0; i < 2;i++)
        {
            t = newThread(Writer);
            t.Name =i.ToString();
            t.Start();
        }
        for(int i = 0; i<3;i++)
        {
            t = newThread(Reader);
            t.Name =i.ToString();
            t.Start();
        }
        Console.ReadLine();
    }
    static void Writer()
    {
        while(true)
        {
            try
            {
                rwLock.AcquireWriterLock(3000);
                Console.WriteLine("writer:"+ Thread.CurrentThread.Name + " is enter" + "WriterSeqNum:" +rwLock.WriterSeqNum.ToString());
                try
                {
                    Thread.Sleep(5000);
                }
                finally
                {
                    rwLock.ReleaseWriterLock();
                    Console.WriteLine("writer:"+ Thread.CurrentThread.Name + " is exit");
                }
            }
            catch(ApplicationException)
            {
                Console.WriteLine("writer:"+ Thread.CurrentThread.Name + " wait time out");
            }
        }
    }
    static void Reader()
    {
        while (true)
        {
            rwLock.AcquireReaderLock(-1);
            Console.WriteLine("reader:"+ Thread.CurrentThread.Name + " is enter" + "WriterSeqNum:" +rwLock.WriterSeqNum.ToString());
            try
            {
                Thread.Sleep(3000);
            }
            finally
            {
                Console.WriteLine("reader:"+ Thread.CurrentThread.Name + " is exit");
                rwLock.ReleaseReaderLock();
            }
        }
    }
    View Code

    7,AutoResetEvent 自动重置事件

    在构造事件对象时需要指定initialState参数是True还是False,以指示事件的初始状态是有信号还是无信号

    当一个自动重置事件得到信号时,等待该事件的线程中只有一个线程变为可调度线程,当手动重置对象得到信号时,等待该事件的所有线程均变为可调度线程

    class Example
    {
        private static AutoResetEvent event_1 = new AutoResetEvent(true);
        private static AutoResetEvent event_2 = new AutoResetEvent(false);
    
        static void Main()
        {
            Console.WriteLine("Press Enter to create three threads and start them.
    " +
                              "The threads wait on AutoResetEvent #1, which was created
    " +
                              "in the signaled state, so the first thread is released.
    " +
                              "This puts AutoResetEvent #1 into the unsignaled state.");
            Console.ReadLine();
    
            for (int i = 1; i < 4; i++)
            {
                Thread t = new Thread(ThreadProc);
                t.Name = "Thread_" + i;
                t.Start();
            }
            Thread.Sleep(250);
    
            for (int i = 0; i < 2; i++)
            {
                Console.WriteLine("Press Enter to release another thread.");
                Console.ReadLine();
                event_1.Set();
                Thread.Sleep(250);
            }
    
            Console.WriteLine("
    All threads are now waiting on AutoResetEvent #2.");
            for (int i = 0; i < 3; i++)
            {
                Console.WriteLine("Press Enter to release a thread.");
                Console.ReadLine();
                event_2.Set();
                Thread.Sleep(250);
            }
    
            // Visual Studio: Uncomment the following line.
            //Console.Readline();
        }
    
        static void ThreadProc()
        {
            string name = Thread.CurrentThread.Name;
    
            Console.WriteLine("{0} waits on AutoResetEvent #1.", name);
            event_1.WaitOne();
            Console.WriteLine("{0} is released from AutoResetEvent #1.", name);
    
            Console.WriteLine("{0} waits on AutoResetEvent #2.", name);
            event_2.WaitOne();
            Console.WriteLine("{0} is released from AutoResetEvent #2.", name);
    
            Console.WriteLine("{0} ends.", name);
        }
    }
    View Code

    8, ManualResetEvent 手动重置事件

    public class Example
    {
        // mre is used to block and release threads manually. It is
        // created in the unsignaled state.
        private static ManualResetEvent mre = new ManualResetEvent(false);
    
        static void Main()
        {
            Console.WriteLine("
    Start 3 named threads that block on a ManualResetEvent:
    ");
    
            for(int i = 0; i <= 2; i++)
            {
                Thread t = new Thread(ThreadProc);
                t.Name = "Thread_" + i;
                t.Start();
            }
    
            Thread.Sleep(500);
            Console.WriteLine("
    When all three threads have started, press Enter to call Set()" +
                              "
    to release all the threads.
    ");
            Console.ReadLine();
    
            mre.Set();
    
            Thread.Sleep(500);
            Console.WriteLine("
    When a ManualResetEvent is signaled, threads that call WaitOne()" +
                              "
    do not block. Press Enter to show this.
    ");
            Console.ReadLine();
    
            for(int i = 3; i <= 4; i++)
            {
                Thread t = new Thread(ThreadProc);
                t.Name = "Thread_" + i;
                t.Start();
            }
    
            Thread.Sleep(500);
            Console.WriteLine("
    Press Enter to call Reset(), so that threads once again block" +
                              "
    when they call WaitOne().
    ");
            Console.ReadLine();
    
            mre.Reset();
    
            // Start a thread that waits on the ManualResetEvent.
            Thread t5 = new Thread(ThreadProc);
            t5.Name = "Thread_5";
            t5.Start();
    
            Thread.Sleep(500);
            Console.WriteLine("
    Press Enter to call Set() and conclude the demo.");
            Console.ReadLine();
    
            mre.Set();
    
            // If you run this example in Visual Studio, uncomment the following line:
            //Console.ReadLine();
        }
    
    
        private static void ThreadProc()
        {
            string name = Thread.CurrentThread.Name;
    
            Console.WriteLine(name + " starts and calls mre.WaitOne()");
    
            mre.WaitOne();
    
            Console.WriteLine(name + " ends.");
        }
    }
    View Code

    9, .NET 在一些集合类,如 Queue、ArrayList、HashTable 和 Stack,已经提供了一个供 lock 使用的对象 SyncRoot。

    Queue q = new Queue(); 
                    lock (q.SyncRoot) 
                    { 
                        foreach (object item in q) 
                        { 
                            //do something 
                        } 
                    }  
    View Code

    参考资料:http://blog.csdn.net/zzy7075/article/details/29842165

  • 相关阅读:
    ZJOI2019 Day1 游记
    Codeforces Round #545 (Div. 1)
    AtCoder WTF 2019 C2. Triangular Lamps Hard
    析合树
    Codeforces Round #539 (Div. 1)
    Codeforces 1103 E. Radix sum
    Codeforces 1097 G. Vladislav and a Great Legend
    sts创建spring项目
    servlet项目demo
    IDEA中创建maven web项目
  • 原文地址:https://www.cnblogs.com/gujf2016/p/6238948.html
Copyright © 2011-2022 走看看