亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

C#生產者/消費者

C#生產者/消費者

交互式愛情 2019-09-20 17:28:54
我最近遇到了生產者/消費者模式c#實現。它非常簡單,(至少對我來說)非常優雅。它似乎是在2006年左右設計的,所以我想知道這種實施是否安全- 仍然適用代碼如下(原始代碼參考http://bytes.com/topic/net/answers/575276-producer-consumer#post2251375)using System;  using System.Collections;  using System.Threading;public class Test{      static ProducerConsumer queue;    static void Main()    {        queue = new ProducerConsumer();        new Thread(new ThreadStart(ConsumerJob)).Start();        Random rng = new Random(0);        for (int i=0; i < 10; i++)        {            Console.WriteLine ("Producing {0}", i);            queue.Produce(i);            Thread.Sleep(rng.Next(1000));        }    }    static void ConsumerJob()    {        // Make sure we get a different random seed from the        // first thread        Random rng = new Random(1);        // We happen to know we've only got 10         // items to receive        for (int i=0; i < 10; i++)        {            object o = queue.Consume();            Console.WriteLine ("\t\t\t\tConsuming {0}", o);            Thread.Sleep(rng.Next(1000));        }    }}public class ProducerConsumer{    readonly object listLock = new object();    Queue queue = new Queue();    public void Produce(object o)    {        lock (listLock)        {            queue.Enqueue(o);            // We always need to pulse, even if the queue wasn't            // empty before. Otherwise, if we add several items            // in quick succession, we may only pulse once, waking            // a single thread up, even if there are multiple threads            // waiting for items.                        Monitor.Pulse(listLock);        }    }
查看完整描述

3 回答

?
月關寶盒

TA貢獻1772條經驗 獲得超5個贊

代碼比那個舊 - 我在.NET 2.0問世之前寫了一段時間。該概念生產者/消費者隊列的方式相比,雖然舊的:)

是的,就我所知,該代碼是安全的 - 但它有一些不足之處:

  • 它不是通用的?,F代版本肯定是通用的。

  • 它無法阻止隊列。停止隊列的一種簡單方法(使所有消費者線程退出)是具有可以放入隊列的“停止工作”令牌。然后,您可以添加與線程一樣多的令牌。或者,您有一個單獨的標志,表示您要停止。(這允許其他線程在完成隊列中的所有當前工作之前停止。)

  • 如果工作量很小,一次只能完成一項工作可能不是最有效的工作。

誠實地說,代碼背后的想法比代碼本身更重要。


查看完整回答
反對 回復 2019-09-20
?
HUWWW

TA貢獻1874條經驗 獲得超12個贊

如果您閱讀評論,您將理解我的答案是錯誤的:)


您的代碼中可能存在死鎖。


想象一下以下情況,為清楚起見,我使用單線程方法,但應該很容易轉換為多線程與睡眠:


// We create some actions...

object locker = new object();


Action action1 = () => {

    lock (locker)

    {

        System.Threading.Monitor.Wait(locker);

        Console.WriteLine("This is action1");

    }

};


Action action2 = () => {

    lock (locker)

    {

        System.Threading.Monitor.Wait(locker);

        Console.WriteLine("This is action2");

    }

};


// ... (stuff happens, etc.)


// Imagine both actions were running

// and there's 0 items in the queue


// And now the producer kicks in...

lock (locker)

{

    // This would add a job to the queue


    Console.WriteLine("Pulse now!");

    System.Threading.Monitor.Pulse(locker);

}


// ... (more stuff)

// and the actions finish now!


Console.WriteLine("Consume action!");

action1(); // Oops... they're locked...

action2();

如果這沒有任何意義,請告訴我。


如果確認了這一點,那么您的問題的答案是“不,這不安全”;)我希望這會有所幫助。


查看完整回答
反對 回復 2019-09-20
  • 3 回答
  • 0 關注
  • 970 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號