而不是開始爲每個文件名一個線程,把文件名到一個隊列,然後啓動三個線程來處理它們。或者,由於主線程現在是免費的,啓動兩個線程,讓它的主線程工作,太:
Queue<string> MyQueue;
void MyProc()
{
string[] filenames = Directory.GetFiles(...);
MyQueue = new Queue(filenames);
// start two threads
Thread t1 = new Thread((ThreadStart)ProcessQueue);
Thread t2 = new Thread((ThreadStart)ProcessQueue);
t1.Start();
t2.Start();
// main thread processes the queue, too!
ProcessQueue();
// wait for threads to complete
t1.Join();
t2.Join();
}
private object queueLock = new object();
void ProcessQueue()
{
while (true)
{
string s;
lock (queueLock)
{
if (MyQueue.Count == 0)
{
// queue is empty
return;
}
s = MyQueue.Dequeue();
}
ProcessFile(s);
}
}
另一種選擇是使用旗語來控制多少線程工作:
Semaphore MySem = new Semaphore(3, 3);
void MyProc()
{
string[] filenames = Directory.GetFiles(...);
foreach (string s in filenames)
{
mySem.WaitOne();
ThreadPool.QueueUserWorkItem(ProcessFile, s);
}
// wait for all threads to finish
int count = 0;
while (count < 3)
{
mySem.WaitOne();
++count;
}
}
void ProcessFile(object state)
{
string fname = (string)state;
// do whatever
mySem.Release(); // release so another thread can start
}
第一個執行會稍微好一些,因爲您沒有爲處理的每個文件名啓動和停止線程的開銷。然而,第二個更短,更清潔,並充分利用了線程池。可能你不會注意到性能差異。
你真的被困在.NET 2.0上嗎?在.NET 3.5中會有更好的方法,或者更好的4.0。 –
@Joe:任何使用ThreadPool或Semaphore的建議? – SAM
不,除非:忘記它,除非您的處理是CPU密集型的。光盤沒有變得更快。 IO將是一個嚴重的瓶頸問題。 – TomTom