C#中lock,Monitor和Mutex的测试代码

来源:互联网 发布:游戏编程入门教程 编辑:程序博客网 时间:2024/05/16 10:01

关于lock,Monitor和Mutex他们同一线程不互斥的测试代码如下:

lock

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;namespace TestThread{    class Program    {        static void Main(string[] args)        {            Thread t1 = new Thread(ThreadFunc1);            Thread t2 = new Thread(ThreadFunc2);            t1.Start();            t2.Start();        }        private static object obj = new object();        private static Mutex m = new Mutex();        static void ThreadFunc1()        {            lock (obj)            {                for (int i = 0; i < 10; i++)                {                    lock (obj)                    {                        Func("ThreadFunc1");                    }                }            }        }        static void ThreadFunc2()        {            lock (obj)            {                for (int i = 0; i < 10; i++)                {                    lock (obj)                    {                        Func("ThreadFunc2");                    }                }            }        }        static void Func(string str)        {            Console.WriteLine("{0} {1}", str, System.DateTime.Now.Millisecond.ToString());            Thread.Sleep(500);         }    }}

Mutex

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;namespace TestThread{    class Program    {        static void Main(string[] args)        {            Thread t1 = new Thread(ThreadFunc1);            Thread t2 = new Thread(ThreadFunc2);            t1.Start();            t2.Start();        }        private static object obj = new object();        private static Mutex m = new Mutex();        static void ThreadFunc1()        {            m.WaitOne();            for (int i = 0; i < 10; i++)            {                m.WaitOne();                Func("ThreadFunc1");                m.ReleaseMutex();            }            m.ReleaseMutex();        }        static void ThreadFunc2()        {            m.WaitOne();            for (int i = 0; i < 10; i++)            {                m.WaitOne();                Func("ThreadFunc2");                m.ReleaseMutex();            }            m.ReleaseMutex();        }        static void Func(string str)        {            Console.WriteLine("{0} {1}", str, System.DateTime.Now.Millisecond.ToString());            Thread.Sleep(500);         }    }}

Monitor

using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threading;namespace TestThread{    class Program    {        static void Main(string[] args)        {            Thread t1 = new Thread(ThreadFunc1);            Thread t2 = new Thread(ThreadFunc2);            t1.Start();            t2.Start();        }        private static object obj = new object();        private static Mutex m = new Mutex();        static void ThreadFunc1()        {            Monitor.Enter(obj);            for (int i = 0; i < 10; i++)            {                Monitor.Enter(obj);                Func("ThreadFunc1");                Monitor.Exit(obj);            }            Monitor.Exit(obj);        }        static void ThreadFunc2()        {            Monitor.Enter(obj);            for (int i = 0; i < 10; i++)            {                Monitor.Enter(obj);                Func("ThreadFunc2");                Monitor.Exit(obj);            }            Monitor.Exit(obj);        }        static void Func(string str)        {            Console.WriteLine("{0} {1}", str, System.DateTime.Now.Millisecond.ToString());            Thread.Sleep(500);         }    }}
0 0
原创粉丝点击