C# - 如何唤醒睡眠线程?

时间:2011-04-14 03:15:52

标签: c# asp.net multithreading mutex monitor

我能够通过进一步阅读来解决这个问题,我觉得你应该引用线程而不是对象。它现在有效。 :)

原帖:


我查看过大多数类似的问题,我看过很多包含显示器,脉冲等的答案,但是我无法使它工作。

我对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秒运行一次。能够让它每秒都能运行会很棒。

1 个答案:

答案 0 :(得分: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)
        }
    }
}