我正在尝试在VB中使用多线程。我有一个程序,它扫描一个非常大的共享区域(200k +文件)并对每个文件执行数字检查。我创建了10个线程,每个线程运行相同的函数,但具有不同的参数。我传递给函数的参数是函数将从中开始并向下挖掘的高级目录路径。我的逻辑是,将函数拆分到较小的目录将比在一个大目录上运行一次更快。然而,这是使用100%的CPU,我确信有一种方法可以节省线程之间的切换等处理能力,但我在网上搜索的所有东西都没有真正帮助。下面是我创建并由线程启动的地方:
Dim TempAreaThread As Thread = New Thread(Sub() Tempchecker(tempDir))
Dim MainAreaThread1 As Thread = New Thread(Sub() MainChecker(dir1))
Dim MainAreaThread2 As Thread = New Thread(Sub() MainChecker(dir2))
Dim MainAreaThread3 As Thread = New Thread(Sub() MainChecker(dir3))
Dim MainAreaThread4 As Thread = New Thread(Sub() MainChecker(dir4))
Dim MainAreaThread5 As Thread = New Thread(Sub() MainChecker(dir5))
Dim MainAreaThread6 As Thread = New Thread(Sub() MainChecker(dir6))
Dim MainAreaThread7 As Thread = New Thread(Sub() MainChecker(dir7))
Dim MainAreaThread8 As Thread = New Thread(Sub() MainChecker(dir8))
Dim MainAreaThread9 As Thread = New Thread(Sub() MainChecker(dir9))
MainAreaThread1.Start()
MainAreaThread2.Start()
MainAreaThread3.Start()
MainAreaThread4.Start()
MainAreaThread5.Start()
MainAreaThread6.Start()
MainAreaThread7.Start()
MainAreaThread8.Start()
MainAreaThread9.Start()
TempAreaThread.Start()
TempAreaThread.Priority = ThreadPriority.AboveNormal
MainAreaThread6.Priority = ThreadPriority.AboveNormal
MainAreaThread8.Priority = ThreadPriority.AboveNormal
如果有人能指出我在正确的方向上更有效地使用线程,那么当我的4核正在挣扎时,它会很棒。我是学生,所以我明白这可能是一个非常天真的问题,而且我对线程的使用是令人震惊的。
谢谢