您的位置:首页 > 编程语言 > C#

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

2015-07-06 11:20 525 查看
关于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); 
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: