2011-05-24 39 views
-1

我想逐個將一個集合的元素出列,然後將它們的值存儲在MYSQL數據庫中。有誰知道如何使用兩個線程逐個處理一個集合?

當第一元素出隊我想啓動一個線程來更新數據庫,然後在第二個元素離隊我要開始另一個線程。

當第一個線程執行結束後,我想重新使用它處理的第三個元素在隊列中。然後,在第二個線程完成執行後,我想重新使用它來處理隊列中的第四個元素。我想繼續這種方式,直到處理完隊列中的所有元素。

這些全部進程在1ms處理另一個線程。

有誰知道如何處理收集逐個使用兩個線程?

+0

???????????????不知道你在說什麼,到目前爲止你嘗試了什麼,你有僞代碼,張貼表格。對我來說,這個問題目前的形式看起來是無法迴避的。 – Johan 2011-05-24 12:20:48

+0

可能想查看'Thread Pool'類:http://msdn.microsoft.com/en-us/library/3dasc8as%28v=vs.80%29.aspx。也許可以多解釋一下你正遇到麻煩的部分。 – klennepette 2011-05-24 12:50:25

+0

問題是什麼?你寫了什麼代碼? – Amy 2011-05-24 14:05:13

回答

2

它看起來很複雜的,沒有必要在你提出確切的方式解決這個問題。本質上,你已經映射出系統中一個線程處理集合中所有具有偶數索引的項目,另一個處理具有奇數索引的所有項目。

不過,如果你真正的目標是利用處理兩個線程隊列,並不要緊哪個線程處理哪個項目,這裏是做一個方式,使用ConcurrentQueue<T>類:

// Queue of items to process 
    static ConcurrentQueue<string> queue; 

    // Random number generator to simulate time it takes to do work. 
    static Random rnd = new Random(); 

    static void Main(string[] args) 
    { 
     // Items to process. 
     List<string> names = new List<string>() { "Joe", "Bob", "Fred", "Jack", "Jill", 
      "Suzy", "Amy", "Alice", "Andy", "Bill", "Chuck" }; 

     // Demonstrates putting the items into a concurrent queue. 
     queue = new ConcurrentQueue<string>(names); 

     // Work threads 
     Thread first = new Thread(AddToDatabase); 
     Thread second = new Thread(AddToDatabase); 

     // Start the work threads 
     first.Start("First"); 
     second.Start("Second"); 
    } 

    // Simulated work routine. 
    static void AddToDatabase(object state) 
    { 
     string name; 
     while (queue.TryDequeue(out name)) 
     { 
      Console.WriteLine(string.Concat(state, ": Putting ", name, " in the database.")); 
      Thread.Sleep(rnd.Next(1000, 5000)); 
     } 
    } 

運行該代碼產生的輸出是這樣的:

First: Putting Joe in the database. 
Second: Putting Bob in the database. 
Second: Putting Fred in the database. 
First: Putting Jack in the database. 
Second: Putting Jill in the database. 
First: Putting Suzy in the database. 
Second: Putting Amy in the database. 
First: Putting Alice in the database. 
Second: Putting Andy in the database. 
First: Putting Bill in the database. 
Second: Putting Chuck in the database. 

正如你可以看到兩個線程都通過隊列工作他們的方式,通常切換回和第四,但沒有保證,他們將在任何特定的順序執行。

然而,即使這個代碼是更多的工作比你真正需要做的。您可以使用並行LINQ,讓框架的決定有多少線程需要:

// Items to process. 
List<string> names = new List<string>() { 
    "Joe", "Bob", "Fred", "Jack", "Jill", "Suzy", "Amy", "Alice", 
    "Andy", "Bill", "Chuck" }; 

// Process using Parallel LINQ 
names.AsParallel().ForAll(name => 
{ 
    var id = Thread.CurrentThread.ManagedThreadId; 
    Console.WriteLine(string.Concat(id, ": Putting ", name, " in the database.")); 
    Thread.Sleep(rnd.Next(1000, 5000)); 
}); 

將會產生類似的輸出:

1: Putting Amy in the database. 
3: Putting Joe in the database. 
1: Putting Alice in the database. 
1: Putting Andy in the database. 
3: Putting Bob in the database. 
3: Putting Fred in the database. 
1: Putting Bill in the database. 
3: Putting Jack in the database. 
1: Putting Chuck in the database. 
3: Putting Jill in the database. 
3: Putting Suzy in the database. 

我已經離開了代碼的部分,將真正把項目在數據庫中。對於如何使用多個線程處理您的集合,這實際上是一個單獨的問題。如果您想了解如何類的信息,如ConcurrentQueue<T>工作,檢查出喬恩斯基特的article on multi-threading in .NET.

+1

+1這是非常徹底的,但您仍然必須警惕[虛假分享](http://drdobbs.com/go-parallel/article/217500206?pgno=1),因爲共享緩存行的潛力該集合,導致連續性表現 – 2011-05-28 05:33:02

+0

@Conrad Frix:感謝有趣的鏈接。 – 2011-05-28 06:00:46

相關問題