.NET平台下的管程类

本文介绍了.NET平台下如何使用管程类进行线程管理,通过C#语言示例展示了管程在处理和打印数据线程间的协作。讨论了Monitor.Pulse和Monitor.Exit等方法在多线程同步中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

这周帮老师做了一个介绍管程的PPT,很多书在讲操作系统中线程的管程时,都有标注上说,管程比较偏学术,没有哪个语言支持,像我的老师就问了很多同事,大家都不知道其实.NET平台已经实现了管程类来帮助我们管理线程。以下我用C#语言作为例子,来简单介绍一下。如果有哪位高手更加了解这个类,希望您可以多讲解一下,如果发现错误请提出!!非常感激~

.NET Framework的System.Threading命名空间下提供了monitor类来实现线程同步。它提供了资源访问的控制机制,允许一个特定的线程对资源的独占式访问。与Lock不同,它可以进一步提供控制线程的具体操作。
下面是对monitor类的方法介绍[1]
Public static void Enter(Object obj)—开始进入监控区,试图得到指定对象的监控器锁。
Public static void Exit(Object obj)—监控结束,并释放指定对象的监控锁。
Public static void Pulse(Object obj)—释放对象的锁,并叫醒等待队列里的第一个线程。
Public static void PulseAll(Object obj) )—释放对象的锁,并叫醒等待队列里的所有线程。
Public static bool TryEnter(Object obj)
Public static bool TryEnter(Object obj,Int32)
Public static bool TryEnter(Object obj,TimeSpan)—试图在特定条件下得到指定对象的监控器锁,参数可带超时时间。
Public static bool Wait(Object obj)
Public static bool Wait (Object obj,Int32)
Public static bool Wait (Object obj,TimeSpan)—释放监控锁,把调用线程放入等待线程队列,参数可带超时时间

 

[3]:在C# 2.0中,Monitor.Enter()和Monitor.Exit()调用间要try/finally来保护,否则可能会导致一个Monitor.Exit()永远不会被调用的异常。

下面,我们来看一个简单的例子[2]:
using System;
using System.Threading;
namespace Test
{
    class UsingMonitor
    {
        private int sum = 0;
        private int output = 0;
        void StartThread()
        {
            Thread Doperation = new Thread(new ThreadStart(DataHandle));
            Thread Dprint = new Thread(new ThreadStart(Dataprint));
            Doperation.Name = "handle thread";
            Dprint.Name = "print thread";
            Dprint.Start();
            Doperation.Start();
            Doperation.Join();
            Dprint.Join();
        }
        void DataHandle()
        {
            Monitor.Enter(this);
            for (int i = 0; i < 20; i++)
            {
                if (output == 10)
                {
                    Monitor.Wait(this);//handle thread释放资源,进入等待列队
                }
                output++;
                Console.WriteLine(Thread.CurrentThread.Name + " is handling the" + output + " data.");
                Thread.Sleep(1000);
                if (output == 10)
                {
                    Monitor.Pulse(this);//handle thread 中的资源被释放,叫醒等待列队中的第一个线程。
                    Console.WriteLine();
                }
            }
            Monitor.Wait(this);
        }
        void Dataprint()
        {
            Monitor.Enter(this);
            do
            {
                if (output == 0)
                {
                    Monitor.Wait(this);//print thread释放资源,进入等待列队
                }
                Console.WriteLine(Thread.CurrentThread.Name + " is printint the" + output + " data.");
                Thread.Sleep(1000);
                output--;
                sum++;
                Console.WriteLine("/t the totle of data:" + sum);

                         if (output == 0)
                {
                    Monitor.Pulse(this););//print thread 中的资源被释放,叫醒等待列队中的第一个线程。

                    Console.WriteLine();
                }
            } while (sum < 20);
            Monitor.Exit(this);

        }

        static void main()
        {
            UsingMonitor myUM = new UsingMonitor ();
            myUM.StartThread();
            Console .Write ("end");
            Console .ReadLine();
        }
          

    }
}
 
 

执行结果:
Data handle thread   is handling the 1 data.
…….
Data handle thread   is handling the 10 data.
Data printe thread is printing the 10 data.       The totle of  data:1
…….
Data printe thread is printing the 1 data.       The totle of  data:10

…….

Data handle thread   is handling the 1 data.
…….
Data handle thread   is handling the 10 data.
Data printe thread is printing the 10 data.       The totle of  data:11
…….
Data printe thread is printing the 1 data.       The totle of  data:20

end

参考资料:
[1]《C# Programmer’s Reference》
清华大学出版社
[2]《Visual C# 范例精要解析》吕高旭 编著
清华大学出版社
[3] 《Essential C# 2.0》 Mark Michealis著
人们邮电出版社

 


 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值