ThreadPool.QueueUserWorkItem vs Parallel.For(ThreadPool.QueueUserWorkItem vs Parallel.For)

编程入门 行业动态 更新时间:2024-10-27 23:23:52
ThreadPool.QueueUserWorkItem vs Parallel.For(ThreadPool.QueueUserWorkItem vs Parallel.For)

我试图理解Parralel.For和ThreadPool.QueueUserWorkItem之间的区别。

硬件软件:

英特尔i5(四核) Windows 7 64位教授 DotNet 4.5

案例1代码:ThreadPool

for (int index = 0; index < 5; index++) { ThreadPool.QueueUserWorkItem((indexParam) => { int threadID = Thread.CurrentThread.ManagedThreadId; Thread.Sleep(1000); BeginInvoke((Action)delegate { listBox1.Items.Add("Completed " + indexParam.ToString() + " using thread " + threadID.ToString() + " (" + DateTime.Now.Second.ToString() + "." + DateTime.Now.Millisecond.ToString("000") + ")"); }); }, index); }

输出:

使用主题10完成0(45.871) 使用第11号线完成1(45.875) 使用12号线完成2(45.875) 使用第13号线完成3(45.875) 使用第10个线程完成4(46.869)

案例2代码:Parallel.For

ParallelLoopResult result = Parallel.For(0, 5, (int index, ParallelLoopState loopState) => { int threadID = Thread.CurrentThread.ManagedThreadId; Thread.Sleep(1000); BeginInvoke((Action)delegate { listBox1.Items.Add("Completed " + index.ToString() + " using thread " + threadID.ToString() + " (" + DateTime.Now.Second.ToString() + "." + DateTime.Now.Millisecond.ToString("000") + ")"); }); });

输出:

使用主题10(16.923)完成0 使用第11号线完成1(16.925) 使用第12个线程完成2(16.925) 使用第13号线完成3(16.926) 使用主题14完成4(16.926)

题:

从案例1中的结果看,似乎只有四个线程处于活动状态,之后第一个空闲线程用于完成最终任务。 在案例2中,看起来五个线程立即被专用并且“同时”执行。 为什么QueueUserWorkItem的线程处理不使用第五个线程,比如并行类?

(ThreadPool.GetAvailableThreads(...)确认1023个工作线程可用)。

I'm trying to understand the differences between Parralel.For and ThreadPool.QueueUserWorkItem.

Hardware & Software:

Intel i5 (quad core) Windows 7 64bit prof DotNet 4.5

Case 1 code: ThreadPool

for (int index = 0; index < 5; index++) { ThreadPool.QueueUserWorkItem((indexParam) => { int threadID = Thread.CurrentThread.ManagedThreadId; Thread.Sleep(1000); BeginInvoke((Action)delegate { listBox1.Items.Add("Completed " + indexParam.ToString() + " using thread " + threadID.ToString() + " (" + DateTime.Now.Second.ToString() + "." + DateTime.Now.Millisecond.ToString("000") + ")"); }); }, index); }

Output:

Completed 0 using thread 10 (45.871) Completed 1 using thread 11 (45.875) Completed 2 using thread 12 (45.875) Completed 3 using thread 13 (45.875) Completed 4 using thread 10 (46.869)

Case 2 code: Parallel.For

ParallelLoopResult result = Parallel.For(0, 5, (int index, ParallelLoopState loopState) => { int threadID = Thread.CurrentThread.ManagedThreadId; Thread.Sleep(1000); BeginInvoke((Action)delegate { listBox1.Items.Add("Completed " + index.ToString() + " using thread " + threadID.ToString() + " (" + DateTime.Now.Second.ToString() + "." + DateTime.Now.Millisecond.ToString("000") + ")"); }); });

Output:

Completed 0 using thread 10 (16.923) Completed 1 using thread 11 (16.925) Completed 2 using thread 12 (16.925) Completed 3 using thread 13 (16.926) Completed 4 using thread 14 (16.926)

Question:

From the results in case 1, it appears that only four threads are active, after which the first free thread is then used to complete the final task. In case 2, it appears five threads are immediately dedicated and execute 'simultaneously'. Why does the thread handling for QueueUserWorkItem not use a fifth thread, like the parallel class?

(ThreadPool.GetAvailableThreads(...) confirms that 1023 worker threads are available).

最满意答案

我相信“活动”线程和“可用”线程之间存在差异。 线程池将重用线程,如果它决定需要更多线程,它将开始将可用线程移动到活动线程。 如果你想一次启动很多东西,这可能是令人沮丧的,因为每个可用的线程可能需要大约2秒才能启动。

MSDN托管Threadpool

作为其线程管理策略的一部分,线程池在创建线程之前会延迟。 因此,当许多任务在短时间内排队时,在所有任务开始之前可能会有很长的延迟。

如果要反复运行这些任务或添加任务,则应该看到其他线程变为活动状态。

http://msdn.microsoft.com/en-us/library/system.threading.threadpool.setminthreads%28v=vs.110%29.aspx

In this case, the ThreadPool min worker threads default is 4 - which explains why only 4 threads are used. The documentation states that ThreadPool may decide whether or not to create more threads once the minimum is reached.

When setting the minimum number of worker threads to 5, then 5 threads are created and all tasks complete simultaneously as expected.

更多推荐

本文发布于:2023-07-31 00:17:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1340409.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:ThreadPool   QueueUserWorkItem   Parallel

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!