我有一个进程,我的主线程正在读取文件并将其拆分为多个部分。那些部件需要进一步处理。我想利用任何可用的线程,以便下游处理尽可能多地使用CPU(或尽可能多的内核)。我不想从主线程创建过多的积压,所以我需要主线程等待添加到队列,直到有另一个可用的线程。
我看到许多文章如VB.NET 4.0: Looking to execute multiple threads, but wait until all threads are completed before resuming,但他们正在等待所有线程完成,而我只需要任何线程可用
这是我可以用Task Parallel Library解决的问题,还是应该手动创建线程并监控线程池?
Using Reader As New StreamReader(FileName)
Do
CurrentBlockSize = Reader.ReadBlock(CurrentBuffer, 0, BufferSize)
RunningBuffer &= New String(CurrentBuffer)
If RunningBuffer.Contains(RowDelimiter) Then
LineParts = RunningBuffer.Split(RowDelimiter)
For I As Integer = 0 To LineParts.Count - 1
If I < LineParts.Count - 1 Then
'Make synchronous call that blocks until'
'another thread is available to process the line'
AddLineToTheProcessingQueue(CurrentLine)
Else
RunningBuffer = LineParts(I)
End If
Next
End If
Loop While CurrentBlockSize = BufferSize
End Using
答案 0 :(得分:1)
将此代码粘贴到新的控制台应用程序中。
Imports System.Threading
Module Module1
' I just picked 6 randomly, not sure what is a good strategy for picking this number
' also, not sure what is the difference between a Worker Thread and a Completion thread
Const MaxWorkerThreads As Integer = 6
Const MaxCompletionPortThreads As Integer = 6
Sub Main()
ThreadPool.SetMaxThreads(MaxWorkerThreads, MaxCompletionPortThreads)
Dim availableWorkerThreads As Integer
Dim availableCompletionPortThreads As Integer
For i As Integer = 0 To 100
' GetAvailableThreads returns results via output parameters
ThreadPool.GetAvailableThreads(availableWorkerThreads, availableCompletionPortThreads)
Dim tries As Integer = 0
Do While (availableWorkerThreads = 0)
' this loop does not execute if there are available threads
' you may want to add a fail-safe to check "tries" in case the child threads get stuck
tries += 1
Console.WriteLine(String.Format("waiting to start item {0}, attempt {1}, available threads: {2}, {3}", i, tries, availableWorkerThreads, availableCompletionPortThreads))
' failure to call Sleep will make your program unresponsive
Thread.Sleep(1000)
' call GetAvailableThreads again for the next test at the top of the loop
ThreadPool.GetAvailableThreads(availableWorkerThreads, availableCompletionPortThreads)
Loop
' this is how you pass parameters to a thread created through QueueUserWorkItem
Dim parameters As Object() = {i}
ThreadPool.QueueUserWorkItem(AddressOf DoWork, parameters)
' According to MSDN, you must Sleep after calling QueueUserWorkItem, or else the current thread will just exit
Thread.Sleep(500)
Next
End Sub
Sub DoWork(parameters As Object())
Dim itemNumber = parameters(0)
Dim sleepLength = itemNumber * 1000
Console.WriteLine(String.Format("Item: {0} - sleeping for {1} miliseconds.", itemNumber, sleepLength))
Thread.Sleep(sleepLength)
Console.WriteLine(String.Format("Item: {0} - done sleeping.", itemNumber))
End Sub
End Module
答案 1 :(得分:0)
我不确定您为什么要这样做,但是您可以使用BlockingCollection
或设置了BoundedCapacity
的数据流块来实现非常相似的功能。
例如,如果您将容量设置为1并且您的消费者此时正忙,则在其中一个消费者完成其当前工作并从中删除该项之前,您将无法将第二个项添加到队列中队列。两个版本都为您提供了一种等待,直到您可以将另一个项目添加到队列中。