首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >this(Monitor.Pulse)不会触发Monitor.Wait(this);

this(Monitor.Pulse)不会触发Monitor.Wait(this);
EN

Stack Overflow用户
提问于 2013-02-22 23:02:51
回答 1查看 252关注 0票数 1

我正在尝试让Monitor.Pulse(this)在我的代码中触发Monitor.Wait(this)。我想我的等待语句都是在没有Pulse的情况下运行的。我有5个不同的线程,由5个不同的对象运行,每个对象代表一个具有不同优先级的队列。我试图让每个线程以特定的优先级运行,而不使用线程优先级属性(即正常、高于正常等)。无论如何,需要指出的是,每个线程只运行一次,然后它们似乎被困在为每个队列运行的线程的Monitor.Wait(这)部分。有人知道为什么Monitor.Pulse(这个)不触发Monitor.Wait(这个)并继续这个循环吗?每个线程都应该由Monitor.Wait(this)和使用全局变量GlobalCount的while循环逐个触发。我认为这个问题一定发生在我的Beta方法中,在第一个类(Msg类)的顶部,触发发生了。或者在我的main方法中,尽管我不太确定这部分是否有问题。

发生的情况是,它将执行几行,然后开始一个新行,但不会打印其他任何内容。代码仍在运行。我还尝试删除Monitor.Pulse和Monitor.Wait,但它部分起作用,但每次增量对象的beta方法运行其线程时,它都被alpha方法取代。有没有人知道这是为什么,以及我如何获得Pulse并等待工作?

下面是我的代码(忽略一些注释):

代码语言:javascript
复制
   // StopJoin.cs
using System;
using System.Threading;
using System.Collections;



public class Msg
{
string message;
int priority;

public Msg(string ms, int pr)
{message = ms;
priority = pr;}


// This method that will be called when the thread is started
public void Beta()
{



while(true){

//Console.WriteLine("asdfasdfs");
Console.WriteLine(message+":"+GlobalClass.globalCount);
lock(this)   // Enter synchronization block
{
while((priority - 1) != GlobalClass.globalCount){
//Console.WriteLine(GlobalClass.globalCount);
try
{
// Waits for the Monitor.Pulse in WriteToCell
//Console.WriteLine("beginning");
//Monitor.Wait(this);
//Console.WriteLine("end");
}
catch (SynchronizationLockException e)
{
Console.WriteLine(e);
}
catch (ThreadInterruptedException e)
{
Console.WriteLine(e);
}
if(GlobalClass.globalCount >= 5)
    GlobalClass.globalCount = 0;
}
Console.WriteLine(message+".Beta is running in its own thread.");

for(int i = 0;i<priority;i++)
{
Console.WriteLine("sending message...");

}

if(GlobalClass.globalCount < 5)
    GlobalClass.globalCount = GlobalClass.globalCount + 1;


//Monitor.Pulse(this);   // Pulse tells Cell.WriteToCell that
//Console.WriteLine(GlobalClass.globalCount);
}
}
}
}






public class Alpha
{
Msg the_message = new Msg("Alpha",1);

public void doWork()
{the_message.Beta();}
};




public class Charlie
{
Msg the_message = new Msg("Charlie",2);
public void doWork()
{the_message.Beta();}
};






public class Delta
{
Msg the_message= new Msg("Alpha",3);
public void doWork()
{the_message.Beta();}
};





public class Echo
{
Msg the_message= new Msg("Echo",4);
public void doWork()
{the_message.Beta();}
};







public class Foxtrot
{
Msg the_message= new Msg("Foxtrot",5);
public void doWork()
{the_message.Beta();}
};




static class GlobalClass
{
private static int global_count = 0;

public static int globalCount
{
get{return global_count;}
set{global_count = value;}
}
}






public class Simple
{
public static int Main()
{

GlobalClass.globalCount = 2;

long s = 0;
long number = 100000000000000000;

Console.WriteLine("Thread Start/Stop/Join Sample");

Alpha oAlpha = new Alpha();
Charlie oCh = new Charlie();
Delta oDe = new Delta();
Echo oEc = new Echo();
Foxtrot oFo = new Foxtrot();

// Create the thread object, passing in the Alpha.Beta method
// via a ThreadStart delegate. This does not start the thread.
Thread oThread = new Thread(new ThreadStart(oAlpha.doWork));
Thread aThread = new Thread(new ThreadStart(oCh.doWork));
Thread bThread = new Thread(new ThreadStart(oDe.doWork));
Thread cThread = new Thread(new ThreadStart(oEc.doWork));
Thread dThread = new Thread(new ThreadStart(oFo.doWork));

// Start the thread
oThread.Start();
aThread.Start();
bThread.Start();
cThread.Start();
dThread.Start();

// Spin for a while waiting for the started thread to become
// alive:
while (!oThread.IsAlive);
while (!aThread.IsAlive);
while (!bThread.IsAlive);
while (!cThread.IsAlive);
while (!dThread.IsAlive);

// Put the Main thread to sleep for 1 millisecond to allow oThread
// to do some work:
Thread.Sleep(1);



// Wait until oThread finishes. Join also has overloads
// that take a millisecond interval or a TimeSpan object.
oThread.Join();
aThread.Join();
bThread.Join();
cThread.Join();
dThread.Join();

Console.WriteLine();
Console.WriteLine("Alpha.Beta has finished");

/*
try 
{
Console.WriteLine("Try to restart the Alpha.Beta thread");
oThread.Start();
}
catch (ThreadStateException) 
{
Console.Write("ThreadStateException trying to restart Alpha.Beta. ");
Console.WriteLine("Expected since aborted threads cannot be restarted.");
}
*/


while(s<number)
s++;

// Request that oThread be stopped
oThread.Abort();
aThread.Abort();
bThread.Abort();
cThread.Abort();
dThread.Abort();


return 0;
}
}
EN

回答 1

Stack Overflow用户

发布于 2013-02-22 23:59:12

我可以看到你的代码中有很多问题,但是有两个主要的问题会影响你。我假设您注释掉的Monitor调用不应该被注释(否则代码就没有意义了)。

首先,在每个线程下创建一个新的Msg实例。Beta方法锁定Msg的当前实例(在注释的Monitor.Wait(this)中),因此每个实例实际上都在等待自己-这将是一个无限的等待,因为唯一的Monitor.Pulse在同一方法中较晚,并且永远不会到达。

因为您的一些Msg实例将使用更高的priority值创建,所以它们将完全跳过while循环,并且应该继续调用Monitor.Pulse,但不会有任何东西等待该脉冲。

稍后在您的Main方法中,您将拥有以下内容:

代码语言:javascript
复制
    while (!oThread.IsAlive) ;
    while (!aThread.IsAlive) ;
    while (!bThread.IsAlive) ;
    while (!cThread.IsAlive) ;
    while (!dThread.IsAlive) ;

这是有缺陷的。因为不能保证线程的执行顺序,所以上面的代码完全有可能死锁。如果您的oThread没有立即启动,但是dThread被调度并运行到完成,那么您很容易看到这样的情况:在到达上面的最后一行之前,dThread已经完成并且“死”了。

总而言之,我不清楚你的代码试图实现什么,但就目前的情况而言,我希望它每次都会死锁。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15027032

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档