Which threads live in the .NET ThreadPool?
There are a few ways to execute asynchronous threads in .NET:
System.Threading.Thread.Start()
System.Delegate.BeginInvoke()
My question is where do asynchronous threads live in each 开发者_运维知识库of these scenarios? I want to in some cases avoid using pooled threads because some processes I'm executing take a long time and will obligate the pooled thread.
Threads that run code called from Delegate.BeginInvoke
exist in the same pool as those accessed from the ThreadPool
class. Creating your own Thread
object does just that: creates a new (non-pooled) thread.
Here's a quick way to verify this: create a new Windows Forms application and put two buttons on the form. Include something like the following code:
private void ThreadStartButton_Click(object sender, EventArgs e)
{
ThreadPool.SetMaxThreads(4, 4);
for (int i = 0; i < 8; ++i)
{
Thread t = new Thread(ShowMessageBox);
t.Start();
}
}
private void DelegateBeginInvokeButton_Click(object sender, EventArgs e)
{
ThreadPool.SetMaxThreads(4, 4);
for (int i = 0; i < 8; ++i)
{
Action action = ShowMessageBox;
action.BeginInvoke(action.EndInvoke, null);
}
}
private void ShowMessageBox()
{
int threadId = Thread.CurrentThread.ManagedThreadId;
MessageBox.Show(threadId.ToString());
}
When you click on the first button (the one whose handler creates new Thread
objects), you should see 8 dialogs pop up all at once. When you click on the second one (which calls BeginInvoke
), you should see up to 4 dialogs pop up. The first four times you dismiss one of these by clicking 'OK', another dialog should pop up (with the same thread id) as the closed dialog's thread is returned to the pool.
When you use Thread.Start(), that thread is seperate from the ThreadPool. When you use BeginInvoke, (IIRC) the call is given to the threadpool. Same with QueueUserWorkItem.
In the first scenario they live nowhere. You are manually creating a new thread. In the second example a thread is drawn from the thread pool if there is one available. You should avoid using threads from the thread pool in heavily multi-threaded applications like ASP.NET for long running operations because you might be jeopardizing threads which are also used to service requests.
The basic rule of thumb is to manually create threads for long running operations but few of them (because creating a thread is time-consuming) and use threads from the pool for multiple short lived operations.
System.Threading.Thread.Start()
Involves creating a thread which is not in the pool.
System.Delegate.BeginInvoke()
would depend on the actual implementation which can be thread pool, background or foreground thread.
If you explain your case, we should be able to help more.
精彩评论