问题描述
我有一个使用5个并发线程执行任务的应用程序。线程将需要从项目列表中读取并选择下一个可用项目。一旦这样做,他们需要向计数器添加一个,以便下一个线程能够选择下一个。我知道我将需要使用诸如BlockingCollection之类的方法来执行此操作,以使2个线程最终不会选择相同的数字,然后两个线程都加一个。
我对这将如何工作有些困惑。我已经声明了新的BlockingCollection对象,但不确定从何处开始?任何帮助深表感谢。谢谢。
解决方法
在我看来,您应该使用ConcurrentQueue(Of T)
。队列的全部意义在于您可以从前面选择下一个项目,因此,如果您使用队列数据结构,则无需增加任何计数器。除了Queue(Of T)
类提供的功能之外,ConcurrentQueue(Of T)
类也是线程安全的。听起来完全符合您的需求。每次需要一个项目时,只需致电TryDequeue
,当没有其他项目时,它将返回False
。
在新的控制台应用程序项目中尝试以下操作,以了解实际的原理:
Imports System.Collections.Concurrent
Imports System.Threading
Module Module1
'A thread-safe queue containing numbers from 1 to 100.
Private numbers As New ConcurrentQueue(Of Integer)(Enumerable.Range(1,100))
'Random number generator.
Private rng As New Random
Sub Main()
'Read the queued numbers using five threads.
For i = 1 To 5
Call New Thread(AddressOf DisplayNumbers).Start()
Next
Console.ReadLine()
End Sub
Private Sub DisplayNumbers()
Dim number As Integer
'Try to get the next number until there are no more.
Do While numbers.TryDequeue(number)
'Display the number and the thread that read it.
Console.WriteLine($"Thread: {Thread.CurrentThread.ManagedThreadId}; Number: {number}")
'Wait a random time period.
Thread.Sleep(rng.Next(500,1000))
Loop
End Sub
End Module