帮助需要Monitor.PulseAll() [英] Help needed in Monitor.PulseAll()

查看:218
本文介绍了帮助需要Monitor.PulseAll()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有谁能够解释我简单的例子来处理Monitor.PulseAll()。我已经从这个stackoverflow.As我是初学者我觉得这些都是我头上了一些例子。


< DIV CLASS =h2_lin>解决方案

怎么样(显示交互):

 静无效的主要()
{
obj对象=新的对象();
Console.WriteLine(主线程想要锁);
锁(OBJ)
{
Console.WriteLine(主线程锁......);
ThreadPool.QueueUserWorkItem(ThreadMethod,OBJ);
Thread.sleep代码(1000);
Console.WriteLine(关于等待主线程......);
Monitor.Wait(OBJ); //这个版本并重新获取锁
Console.WriteLine(主线程醒了);
}
Console.WriteLine(主线程释放锁);
}
静态无效ThreadMethod(obj对象)
{
Console.WriteLine(池线程想要锁);
锁(OBJ)
{
Console.WriteLine(池线程锁);
Console.WriteLine((按回车键));
到Console.ReadLine();
Monitor.PulseAll(OBJ); //此信号,但不释放锁
Console.WriteLine(池线程脉冲);
}
Console.WriteLine(池线程释放锁);
}






再信号;与显示器(又名锁定)的关系时,有两种类型的阻塞;有就绪队列,其中的线程在排队等待执行。后 Console.WriteLine(池线程想要锁)行; 池队列进入就绪队列。当锁被释放一个线程从就绪队列中获取该锁



第二个队列是需要清醒线程;调用等待放置在第二队列中的线程(并释放锁是暂时的)。到 PulseAll 呼叫转移,从第二个队列中的所有线程进入就绪队列(脉冲移动只有一个线程),所以。当池中的线程释放锁主线程允许再次拿起锁



这听起来很复杂(也许是) - 但它不是那么糟糕,因为它的声音...诚实。但是,线程代码的总是的技巧,而且需要既谨慎和清醒的头脑接近。


can anybody explain me with simple example to handle Monitor.PulseAll().I have already gone some examples from this stackoverflow.As i am a beginner i feel those are above my head.

解决方案

How about (to show the interaction):

static void Main()
{
    object obj = new object();
    Console.WriteLine("Main thread wants the lock");
    lock (obj)
    {
        Console.WriteLine("Main thread has the lock...");
        ThreadPool.QueueUserWorkItem(ThreadMethod, obj);
        Thread.Sleep(1000);
        Console.WriteLine("Main thread about to wait...");
        Monitor.Wait(obj); // this releases and re-acquires the lock
        Console.WriteLine("Main thread woke up");
    }
    Console.WriteLine("Main thread has released the lock");
}
static void ThreadMethod(object obj)
{
    Console.WriteLine("Pool thread wants the lock");
    lock (obj)
    {
        Console.WriteLine("Pool thread has the lock");
        Console.WriteLine("(press return)");
        Console.ReadLine();
        Monitor.PulseAll(obj); // this signals, but doesn't release the lock
        Console.WriteLine("Pool thread has pulsed");
    }
    Console.WriteLine("Pool thread has released the lock");
}


Re signalling; when dealing with Monitor (aka lock), there are two types of blocking; there is the "ready queue", where threads are queued waiting to execute. On the line after Console.WriteLine("Pool thread wants the lock"); the pool queue enters the ready queue. When the lock is released a thread from the ready queue can acquire the lock.

The second queue is for threads that need waking; the call to Wait places the thread in this second queue (and releases the lock temporarily). The call to PulseAll moves all threads from this second queue into the ready queue (Pulse moves only one thread), so that when the pool thread releases the lock the main thread is allowed to pick up the lock again.

It sounds complex (and perhaps it is) - but it isn't as bad as it sounds... honestly. However, threading code is always tricky, and needs to be approached with both caution and a clear head.

这篇关于帮助需要Monitor.PulseAll()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆