我继承了下面的一些工作正常的代码,唯一的问题是,我希望能够限制任何时候运行的线程数,目前我有大约 8-10 个并行运行的作业,我想要随时将线程数限制为 3 或 4,这是否可以使用下面的代码而无需太多更改?
Public Sub threading_Example()
Dim threadList As New List(Of Thread)
For n = 0 To numJobs 'about 8-10 normally
Dim new_job As New System.Threading.Thread(New System.Threading.ParameterizedThreadStart(AddressOf do_Job))
Dim job As New Job
'job.stuff = stuff
newJob.Start(job)
threadList.Add(new_job)
Next
For Each t As Thread In threadList
t.Join()
Next
End Sub
Public Sub do_Job(ByVal job As Object)
'do task
End Sub