简体   繁体   English

Thread.Sleep()的Tasks / Thread / Delegate.beginInvoke

[英]Tasks/Thread/Delegate.beginInvoke with Thread.Sleep()

In the example below only _DisplayUsingThreads(timesToDisplay) truly do operation in parallel. 在下面的示例中,只有_DisplayUsingThreads(timesToDisplay)才能真正并行执行操作。 While the other two _DisplayUsingTasks and _DisplayUsingDelegates only do 4 at a time (on a quad core machine) and then wait for a second before doing 4 more. 而另外两个_DisplayUsingTasks和_DisplayUsingDelegates一次只做4个(在四核机器上)然后等待一秒再做4个。 Why? 为什么?

public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            delegate void DisplayDelegate();
            DisplayDelegate myDisplaySleep;

            private void btnGo_Click(object sender, EventArgs e)
            {
              const int timesToDisplay = 50;

                //_DisplayUsingDelegates(timesToDisplay);
                //_DisplayUsingTasks(timesToDisplay);
                //_DisplayUsingThreads(timesToDisplay);
            }

            private void _DisplayUsingTasks(int displayNumber)
            {
                for (int i = 0; i < displayNumber; i++)
                {
                    Task task = new Task(DisplayIdSleep);
                    task.Start();
                }

            }

            private void _DisplayUsingThreads(int displayNumber)
            {
                for (int i = 0; i < displayNumber; i++)
                {
                    Thread thread = new Thread(DisplayIdSleep);
                    thread.Start();
                }
            }
            private void _DisplayUsingDelegates(int displayNumber)
            {
                myDisplaySleep = DisplayIdSleep;

                for (int i = 0; i < displayNumber; i++)
                {
                    myDisplaySleep.BeginInvoke(null, null);
                }
            }

            private void DisplayIdSleep()
            {
                Debug.WriteLine("Thread Id : {0}", Thread.CurrentThread.ManagedThreadId);
                Thread.Sleep(1000);
            }

        }

This is a result of Task and BeginInvoke using thread pool threads, whereas creating your own thread, well, creates a thread. 这是使用线程池线程的TaskBeginInvoke的结果,而创建自己的线程则创建一个线程。

Add ThreadPool.SetMinThreads(50, 0); 添加ThreadPool.SetMinThreads(50, 0); to btnGo_Click and observe what happens. btnGo_Click并观察会发生什么。

The number of operations that can be queued to the thread pool is limited only by available memory; 可以排队到线程池的操作数仅受可用内存的限制; however, the thread pool limits the number of threads that can be active in the process simultaneously. 但是,线程池会限制同时在进程中处于活动状态的线程数。 Beginning with the .NET Framework 4, the default size of the thread pool for a process depends on several factors, such as the size of the virtual address space. 从.NET Framework 4开始,进程的线程池的默认大小取决于多个因素,例如虚拟地址空间的大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM