C# - 如何唤醒睡眠线程?
问题描述:
我可以通过进一步阅读来解决这个问题,我想你应该参考线索而不是物件。它现在有效。 :)C# - 如何唤醒睡眠线程?
原帖:
我看着到大多数的类似的问题,我已经看到了很多包含显示器,豆类等的答案,但是我不能使它工作。
我对C#很陌生,所以如果我使用线程错误,请原谅我。但我的问题如下:
我有4个线程,其中一个将三个不同表中的一个整数减少一个。然后根据是否有任何值达到零来执行动作的3个不同线程。
我想要做的是让线程倒计时只唤醒三个其他人的正确线程,如果一些东西达到零。检查这不是问题,问题是要唤醒线程。我现在有一个工作,他们都只是一次运行,但我想用它来使其效率。
这里是我使用的代码,为了简单起见,我只包含了一个代码,它也是一样的想法。这是我应该如何使用它?
这是我通过阅读谷歌上的例子和结果得到的。
public class UpdateQueueSuggestion
{
public void startGame()
{
Thread Check = new Thread(new ThreadStart(checkUpdate));
Check.Start();
}
public void checkUpdate()
{
// (...) Some intialization of variables
// Create the thread used for when entries in the Queue table reaches 0.
Thread Build = new Thread(new ThreadStart(performBuildingUpdate));
Build.Start();
// Some sqlcommands.
while (true)
{
try
{
// Enter to synchronize, if not it yields
// Object synchronization method was called from an unsynchronized block of code.
Monitor.Enter(this);
connection.Open();
// Execute commands. Get COUNT(*) for results reaching 0 in tables and save int in i.
if (i > 0)
{
// Pulse the thread called Build, in order to wake it again.
Monitor.Pulse(Build);
}
}
finally
{
// Exit monitor.
Monitor.Exit(this);
connection.Close();
}
// This one is supposed to run each second, decrement and check for zeros.
// Works just fine if I just put everything in here without monitors,
// but as I said I want to split it up for efficiency.
Thread.Sleep(1000);
}
}
public void performBuildingUpdate()
{
// Some sqlcommands
while (true)
{
Monitor.Enter(this);
try
{
connection.Open();
// Execute commands.
}
finally
{
connection.Close();
Monitor.Wait(this);
Monitor.Exit(this);
}
}
}
}
任何帮助感激,谢谢。
如果有人想知道,这是针对我们在学校为C#课程开发的浏览器游戏项目。这应该是游戏事件的大致时间。目前我们正在使用一个线程中的所有命令并每10秒运行一次。如果能够每秒运行一次,那将是非常棒的。
答
要做到这一点最直接的方法是拥有三个单独的锁定对象,每个工作线程将等待特定的锁定,并且控制线程将根据递减的值脉冲锁定正确的锁定。
public class Controller
{
private object _lock1;
private object _lock2;
private object _lock3;
private Thread _worker1;
private Thread _worker2;
private Thread _worker3;
public Controller()
{
_lock1 = new object();
_lock2 = new object();
_lock3 = new object();
_worker1 = new Thread(ExecuteWorker, _lock1);
_worker2 = new Thread(ExecuteWorker, _lock2);
_worker3 = new Thread(ExecuteWorker, _lock3);
lock (_lock1)
{
_worker1.Start();
Monitor.Wait(_lock1);
}
lock (_lock2)
{
_worker2.Start();
Monitor.Wait(_lock2);
}
lock (_lock3)
{
_worker3.Start();
Monitor.Wait(_lock3);
}
}
public void Execute()
{
// Perform database logic, assuming decremented values are stored.
int value1 = DecrementValueFromDB();
int value2 = DecrementValueFromDB();
int value3 = DecrementValueFromDB();
if (value1 == 0)
{
lock (_lock1)
{
Monitor.Pulse(_lock1);
}
}
if (value2 == 0)
{
lock (_lock2)
{
Monitor.Pulse(_lock2);
}
}
/// ... repeat for third value
//
}
}
每个工人正在运行是这样的:
static void ExecuteWorker(object lockObject)
{
lock (lockObject)
{
// Signal to thread creator that the thread is running.
Montior.Pulse(lockObject);
// Keep running while we have not been marked to shut down
while (!complete)
{
Monitor.Wait(lockObject);
// logic to execute when signaled (value == 0)
}
}
}
建议您阅读这个优秀的资源:http://www.albahari.com/threading/ – 2011-04-14 03:22:38
请不要使用前缀,而不是解决标志标题答案(甚至在需要时添加) – 2011-04-14 06:24:16