C# - 如何唤醒休眠的线程?

3

我通过进一步的阅读自己解决了这个问题,我以为你应该引用线程而不是对象。现在它可以工作了。:)

原帖:


我研究了大部分类似的问题,并且看到了很多包含监视器、脉冲等答案。然而我无法使它工作。

我对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秒运行一次。如果能够使其每秒运行一次将会很好。


3
建议您阅读这个优秀的资源:http://www.albahari.com/threading/。 - Mitch Wheat
请不要在标题前加上“已解决”,而是在答案处标记(必要时添加)。 - Emond
1个回答

0
最直接的方法是使用三个单独的锁对象,每个工作线程将等待特定的锁,控制线程将根据递减的值脉冲正确的锁。
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)
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接