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

时间:2013-02-22 15:02:51

标签: c#

我正在尝试让Monitor.Pulse(this)在我的代码中触发Monitor.Wait(this)。我认为我的Wait语句都在没有Pulse的情况下运行。我有5个不同的线程运行5个不同的线程,每个对象代表一个具有不同优先级的队列。我试图让每个线程以一定的优先级运行,而不使用线程优先级属性(即正常,abovenormal等)。无论如何,重点是每个线程只运行一次然后它们似乎停留在每个队列运行的线程中的Monitor.Wait(this)部分。有谁知道为什么Monitor.Pulse(this)不会触发Monitor.Wait(this)并继续循环。应该通过Monitor.Wait(this)和使用Global变量GlobalCount的while循环一个接一个地触发每个线程。我认为问题必定发生在我的Beta方法中,在此触发发生的顶部的第一个类(Msg类)中。或者在我的主要方法中,虽然我不太确定那个部分有问题。

它会执行几行,然后开始一个新行,但不会打印任何其他内容。代码仍在运行。我也尝试删除Monitor.Pulse和Monitor.Wait并且它部分工作,但每次delta对象的beta方法运行其线程时,它都被alpha方法替换。有谁知道这是为什么以及如何让Pulse和等待工作?

这是我的代码(忽略一些评论):

   // 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;
}
}

1 个答案:

答案 0 :(得分:2)

我可以看到您的代码存在许多问题,但有两个主要问题会对您产生影响。我假设您的注释掉的Monitor调用不应该被评论(否则代码没有意义)。

首先,在每个线程下创建一个Msg的新实例。 Beta方法锁定Msg的当前实例(在注释的Monitor.Wait(this)中),因此每个实例本质上都在等待 - 这将是无限等待,因为唯一{ {1}}稍后使用相同的方法,永远不会到达。

由于某些Monitor.Pulse个实例的创建值较高Msg,因此他们会完全跳过priority循环,并应继续调用while,但没有什么可以等待那个脉冲。

稍后在Monitor.Pulse方法中,您有以下内容:

Main

这是有缺陷的。因为无法保证线程的执行顺序,所以上述代码完全有可能死锁。如果您的 while (!oThread.IsAlive) ; while (!aThread.IsAlive) ; while (!bThread.IsAlive) ; while (!cThread.IsAlive) ; while (!dThread.IsAlive) ; 未立即启动,但oThread已安排并运行完成,您可以轻松查看dThread已完成并且“死”的情况,然后才能执行上一行到达。

总而言之,我不清楚你的代码试图实现什么,但是我认为它每次都会陷入僵局。