C# Monitor类在多线程同步中的应用

(一)C#语言中线程生命周期及状态变迁

image

image
代码
public static class ThreadExt
{
    public static void WriteLine(this Thread t, string remark, params Thread[] threads)
    {
        DateTime dt = DateTime.Now;
        System.Diagnostics.StackFrame stf = new System.Diagnostics.StackTrace(true).GetFrame(1);
        StringBuilder sb = new StringBuilder();
        foreach (Thread item in threads)
        {
            sb.AppendFormat("[线程名:{0,-5} ID:{1} 状态:{2,-13} 优先级:{3}] ", item.Name, item.ManagedThreadId, item.ThreadState, item.Priority);
        }
        Console.WriteLine("{0:HH:mm:ss.fff} L:{1,-4}{2,-12}\t当前线程名:{3,-5} ID:{4} 状态:{5,-13} 优先级:{6}\t{7} ",
            dt, stf.GetFileLineNumber(), remark, t.Name, t.ManagedThreadId, t.ThreadState, t.Priority, sb.ToString());
    }
}
class MonitorSample
{
    private static readonly object _syncObj = new Object();

    static void P(object mainObj)
    {
        Thread main = mainObj as Thread;
        Thread.CurrentThread.WriteLine("P:lock()前 ", main);
        lock (_syncObj)
        {
            Thread.CurrentThread.WriteLine("P:进入Lock ", main);
            Thread.Sleep(1000);
            Thread.CurrentThread.WriteLine("P:开始Wait", main);
            Monitor.Wait(_syncObj);
            Thread.CurrentThread.WriteLine("P:结束Wait ", main);
            Thread.Sleep(4000);
            Thread.CurrentThread.WriteLine("P:退出Lock ", main);
        }
        Thread.CurrentThread.WriteLine("P:结束    ", main);
    }

    static void Main(string[] args)
    {
        Thread.CurrentThread.Priority = ThreadPriority.Highest;
        Thread t = new Thread(new ParameterizedThreadStart(P));
        t.Name = "P";
        t.Priority = ThreadPriority.Lowest;
        Thread.CurrentThread.Name = "Main";
        t.Start(Thread.CurrentThread);
        Thread.CurrentThread.WriteLine("Main:lock()前   ", t);
        lock (_syncObj)
        {
            Thread.CurrentThread.WriteLine("Main:进入Lock   ", t);
            Thread.Sleep(2000);
            Thread.CurrentThread.WriteLine("Main:开始Pulse   ", t);
            Monitor.Pulse(_syncObj);
            Thread.Sleep(3000);
            Thread.CurrentThread.WriteLine("Main:退出Lock   ", t);
        }
        Thread.CurrentThread.WriteLine("Main:结束   ", t);
    }
}

image

image

(一)Wait()和Pulse()机制

代码
class MonitorSample
{
    const int MAX_LOOP_TIME = 5;
    Queue m_smplQueue;

    public MonitorSample()
    {
        m_smplQueue = new Queue();
    }
    public void FirstThread()
    {
        int counter = 0;
        lock (m_smplQueue)
        {
            while (counter < MAX_LOOP_TIME)
            {
                Console.WriteLine("FirstThread 1 Wait() 前");
                Monitor.Wait(m_smplQueue);
                Console.WriteLine("FirstThread 2 Wait() 后");

                m_smplQueue.Enqueue(counter);

                Console.WriteLine("FirstThread 3 Pulse() 前");
                Monitor.Pulse(m_smplQueue);
                Console.WriteLine("FirstThread 4 Pulse() 后");

                counter++;
                Console.WriteLine("FirstThread 5 Pulse() 后 counter={0}", counter);
            }
        }
    }
    public void SecondThread()
    {
        lock (m_smplQueue)
        {
            Console.WriteLine("SecondThread 1 Pulse() 前");
            Monitor.Pulse(m_smplQueue);
            Console.WriteLine("SecondThread 2 Pulse() 后");
            Console.WriteLine("SecondThread 3 Wait() 前");
            while (Monitor.Wait(m_smplQueue, 1000))
            {
                Console.WriteLine("SecondThread 4 Wait() 后");
                int counter = (int)m_smplQueue.Dequeue();
                Console.WriteLine("SecondThread 5 Pulse() 前");
                Monitor.Pulse(m_smplQueue);
                Console.WriteLine("SecondThread 6 Pulse() 后");
            }
        }
    }
    public int GetQueueCount()
    {
        return m_smplQueue.Count;
    }

    static void Main(string[] args)
    {
        MonitorSample test = new MonitorSample();
        Thread tFirst = new Thread(new ThreadStart(test.FirstThread));
        Thread tSecond = new Thread(new ThreadStart(test.SecondThread));
        tFirst.Start();
        tSecond.Start();
        tFirst.Join();
        tSecond.Join();
        Console.WriteLine("Queue Count = " + test.GetQueueCount().ToString());
        Console.ReadKey();
    }
}

代码输出如下:

image

(二)手控同步

代码using System;
using System.Threading;
namespace ManualResetEvent
{
    class Program
    {
        static void Main(string[] args)
        {
            ManualResetEvent mansig;
            bool isState = true;
            mansig = new ManualResetEvent(isState);
            Console.WriteLine("ManualResetEvent 调用 WaitOne 前 信号状态:{0}\t时间:{1}", isState.ToString(), DateTime.Now.ToString("HH:mm:ss.fff"));
            bool state = mansig.WaitOne(5000, false);
            Console.WriteLine("ManualResetEvent 调用 WaitOne 后 信号状态:{0}\t时间:{1}", state, DateTime.Now.ToString("HH:mm:ss.fff"));
            Console.WriteLine("--------------------");
            Console.ReadKey();
        }
    }
}

image
ManualResetEvent Reset() Set()方法using System;
using System.Threading;
namespace ManualResetEvent
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            ManualResetEvent manRE;
            bool isSingle = true;
            manRE = new ManualResetEvent(isSingle);
            Console.WriteLine("ManualResetEvent 调用       WaitOne 前状态: {0}\t时间:{1}", isSingle.ToString(), DateTime.Now.ToString("HH:mm:ss.fff"));
            bool state = manRE.WaitOne(5000, true);
            Console.WriteLine("ManualResetEvent 调用第一次 WaitOne 后状态: {0}\t时间:{1}", state, DateTime.Now.ToString("HH:mm:ss.fff"));
            //state = manRE.Reset();
            manRE.Reset();
            state = manRE.WaitOne(5000, true);
            Console.WriteLine("ManualResetEvent 调用第二次 WaitOne 后状态: {0}\t时间:{1}", state, DateTime.Now.ToString("HH:mm:ss.fff"));
            Console.WriteLine("--------------------");
            Console.ReadKey();
        }
    }
}

image
代码using System;
using System.Threading;
namespace AutoResetEvent
{
    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            AutoResetEvent aRE;
            bool isState = false;
            aRE = new AutoResetEvent(isState);
            Console.WriteLine("AutoResetEvent 调用第一次 WaitOne 前状态: {0}\t时间:{1}", isState.ToString(), DateTime.Now.ToString("HH:mm:ss.fff"));
            bool state = aRE.WaitOne(5000, true);
            Console.WriteLine("AutoResetEvent 调用第一次 WaitOne 后状态: {0}\t时间:{1}", state, DateTime.Now.ToString("HH:mm:ss.fff"));
            state = aRE.WaitOne(5000, true);
            Console.WriteLine("AutoResetEvent 调用第二次 WaitOne 后状态: {0}\t时间:{1}", state, DateTime.Now.ToString("HH:mm:ss.fff"));
            Console.WriteLine("--------------------");
            Console.ReadKey();
        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值