.NET 线程池中存在哪些线程?

发布于 2024-10-10 14:08:53 字数 181 浏览 7 评论 0原文

在 .NET 中有几种执行异步线程的方法:

System.Threading.Thread.Start()

System.Delegate.BeginInvoke()

我的问题是异步线程在每个场景中位于何处?在某些情况下,我想避免使用池线程,因为我正在执行的某些进程需要很长时间并且会占用池线程。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

残疾 2024-10-17 14:08:53

运行从 Delegate.BeginInvoke 调用的代码的线程与从 ThreadPool 类访问的线程存在于同一池中。创建您自己的 Thread 对象的作用就是:创建一个新的(非池化)线程。

下面是验证这一点的快速方法:创建一个新的 Windows 窗体应用程序并在窗体上放置两个按钮。包含类似以下代码的内容:

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());
}

当您单击第一个按钮(其处理程序创建新 Thread 对象的按钮)时,您应该会看到同时弹出 8 个对话框。当您单击第二个(调用 BeginInvoke)时,您应该会看到最多弹出 4 个对话框。前四次您通过单击“确定”关闭其中一个对话框时,当关闭的对话框的线程返回到池中时,应该会弹出另一个对话框(具有相同的线程 ID)。

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.

葬シ愛 2024-10-17 14:08:53

当您使用 Thread.Start() 时,该线程与线程池是分开的。当您使用 BeginInvoke 时,(IIRC) 调用将交给线程池。与 QueueUserWorkItem 相同。

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.

待"谢繁草 2024-10-17 14:08:53

在第一种情况下,他们无处居住。您正在手动创建一个新线程。在第二个示例中,如果有可用线程,则会从线程池中抽取一个线程。您应该避免在 ASP.NET 等大量多线程应用程序中使用线程池中的线程来进行长时间运行的操作,因为您可能会危及也用于服务请求的线程。

基本经验法则是为长时间运行的操作手动创建线程,但数量很少(因为创建线程非常耗时),并使用池中的线程来执行多个短期操作。

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.

行至春深 2024-10-17 14:08:53
System.Threading.Thread.Start()

涉及创建不在池中的线​​程。

System.Delegate.BeginInvoke() 将取决于实际的实现,可以是线程池、后台或前台线程。

如果您解释一下您的情况,我们应该能够提供更多帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文