等待 ThreadPool 中的所有线程完成其工作

发布于 2024-08-26 19:21:21 字数 313 浏览 6 评论 0原文

我有这段代码:

var list = new List<int>();
for(int i=0;i<10;i++) list.Add(i); 
for(int i=0;i<10;i++)
{
     ThreadPool.QueueUserWorkItem(
         new WaitCallback(x => {
             Console.WriteLine(x);  
         }), list[i]);
} 

我想知道所有线程池线程何时完成其工作。我怎样才能做到这一点?

i have this code:

var list = new List<int>();
for(int i=0;i<10;i++) list.Add(i); 
for(int i=0;i<10;i++)
{
     ThreadPool.QueueUserWorkItem(
         new WaitCallback(x => {
             Console.WriteLine(x);  
         }), list[i]);
} 

And i want to know when all threadpools threads finished their work. How i can to do that?

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

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

发布评论

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

评论(6

荒芜了季节 2024-09-02 19:21:21

您需要自己跟踪这一点。

一种选择是使用计数器和重置事件:

int toProcess = 10;
using(ManualResetEvent resetEvent = new ManualResetEvent(false))
{
    var list = new List<int>();
    for(int i=0;i<10;i++) list.Add(i); 
    for(int i=0;i<10;i++)
    {
        ThreadPool.QueueUserWorkItem(
           new WaitCallback(x => {
              Console.WriteLine(x);  
              // Safely decrement the counter
              if (Interlocked.Decrement(ref toProcess)==0)
                 resetEvent.Set();

           }),list[i]);
    } 

    resetEvent.WaitOne();
}
// When the code reaches here, the 10 threads will be done
Console.WriteLine("Done");

You'll need to track this yourself.

One option for this is to use a counter and a reset event:

int toProcess = 10;
using(ManualResetEvent resetEvent = new ManualResetEvent(false))
{
    var list = new List<int>();
    for(int i=0;i<10;i++) list.Add(i); 
    for(int i=0;i<10;i++)
    {
        ThreadPool.QueueUserWorkItem(
           new WaitCallback(x => {
              Console.WriteLine(x);  
              // Safely decrement the counter
              if (Interlocked.Decrement(ref toProcess)==0)
                 resetEvent.Set();

           }),list[i]);
    } 

    resetEvent.WaitOne();
}
// When the code reaches here, the 10 threads will be done
Console.WriteLine("Done");
没有伤那来痛 2024-09-02 19:21:21

在 .NET Framework 4+ 中,使用方便的 System.Threading.CountdownEvent 类:

const int threadCount = 10;
var list = new List<int>(threadCount);
for (var i = 0; i < threadCount; i++) list.Add(i);

using (var countdownEvent = new CountdownEvent(threadCount))
{
    for (var i = 0; i < threadCount; i++)
        ThreadPool.QueueUserWorkItem(
            x =>
            {
                Console.WriteLine(x);
                countdownEvent.Signal();
            }, list[i]);

    countdownEvent.Wait();
}
Console.WriteLine("done");

In .NET Framework 4+ use the handy System.Threading.CountdownEvent class:

const int threadCount = 10;
var list = new List<int>(threadCount);
for (var i = 0; i < threadCount; i++) list.Add(i);

using (var countdownEvent = new CountdownEvent(threadCount))
{
    for (var i = 0; i < threadCount; i++)
        ThreadPool.QueueUserWorkItem(
            x =>
            {
                Console.WriteLine(x);
                countdownEvent.Signal();
            }, list[i]);

    countdownEvent.Wait();
}
Console.WriteLine("done");
痞味浪人 2024-09-02 19:21:21

我不确定 ThreadPool 是否公开了此类功能,但您可以使用等待句柄,顺便说一下,迭代两次似乎没有必要:

var events = new ManualResetEvent[10];
var list = new List<int>();
for (int i = 0; i < 10; i++)
{
    list.Add(i);
    events[i] = new ManualResetEvent(false);
    int j = i;
    ThreadPool.QueueUserWorkItem(x => {
        Console.WriteLine(x);
        events[j].Set();
    }, list[i]);
}
WaitHandle.WaitAll(events);

I am not sure if ThreadPool exposes such functionality but you can use wait handles and by the way iterating twice seems unnecessary:

var events = new ManualResetEvent[10];
var list = new List<int>();
for (int i = 0; i < 10; i++)
{
    list.Add(i);
    events[i] = new ManualResetEvent(false);
    int j = i;
    ThreadPool.QueueUserWorkItem(x => {
        Console.WriteLine(x);
        events[j].Set();
    }, list[i]);
}
WaitHandle.WaitAll(events);
李白 2024-09-02 19:21:21

我就是这样做的。

class Program
{
    static void Main(string[] args)
    {
        var items = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        using (var countdown = new Countdown(items.Length))
        {
            foreach (var item in items)
            {
                ThreadPool.QueueUserWorkItem(o =>
                {
                    Thread.SpinWait(100000000);
                    Console.WriteLine("Thread Done!");
                    countdown.Signal();
                });
            }
            countdown.Wait();
        }
        Console.WriteLine("Job Done!");
        Console.ReadKey();
    }

    public class Countdown : IDisposable
    {
        private readonly ManualResetEvent done;
        private readonly int total;
        private volatile int current;

        public Countdown(int total)
        {
            this.total = total;
            current = total;
            done = new ManualResetEvent(false);
        }

        public void Signal()
        {
            lock (done)
            {
                if (current > 0 && --current == 0)
                    done.Set();
            }
        }

        public void Wait()
        {
            done.WaitOne();
        }

        public void Dispose()
        {
            done.Dispose();
        }
    }
} 

This is how I would do it.

class Program
{
    static void Main(string[] args)
    {
        var items = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
        using (var countdown = new Countdown(items.Length))
        {
            foreach (var item in items)
            {
                ThreadPool.QueueUserWorkItem(o =>
                {
                    Thread.SpinWait(100000000);
                    Console.WriteLine("Thread Done!");
                    countdown.Signal();
                });
            }
            countdown.Wait();
        }
        Console.WriteLine("Job Done!");
        Console.ReadKey();
    }

    public class Countdown : IDisposable
    {
        private readonly ManualResetEvent done;
        private readonly int total;
        private volatile int current;

        public Countdown(int total)
        {
            this.total = total;
            current = total;
            done = new ManualResetEvent(false);
        }

        public void Signal()
        {
            lock (done)
            {
                if (current > 0 && --current == 0)
                    done.Set();
            }
        }

        public void Wait()
        {
            done.WaitOne();
        }

        public void Dispose()
        {
            done.Dispose();
        }
    }
} 
北方的韩爷 2024-09-02 19:21:21

线程池不会告诉您线程何时完成执行,因此工作项必须自行执行。我把代码改成这样:

    var list = new List<int>();
    ManualResetEvent[] handles = new ManualResetEvent[10];
    for (int i = 0; i < 10; i++) {
        list.Add(i);
        handles[i] = new ManualResetEvent(false);
    }
    for (int i = 0; i < 10; i++) {
        ThreadPool.QueueUserWorkItem(
         new WaitCallback(x =>
         {
             Console.WriteLine(x);
             handles[(int) x].Set();
         }), list[i]);
    }

    WaitHandle.WaitAll(handles);

The thread pool does not tell you when the thread has finished executing, so the work item must do it itself. I changed the code like this:

    var list = new List<int>();
    ManualResetEvent[] handles = new ManualResetEvent[10];
    for (int i = 0; i < 10; i++) {
        list.Add(i);
        handles[i] = new ManualResetEvent(false);
    }
    for (int i = 0; i < 10; i++) {
        ThreadPool.QueueUserWorkItem(
         new WaitCallback(x =>
         {
             Console.WriteLine(x);
             handles[(int) x].Set();
         }), list[i]);
    }

    WaitHandle.WaitAll(handles);
未央 2024-09-02 19:21:21
    static void Main(string[] args)
    {
        for (int i = 0; i < 10; i++)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(xyz));
        }

        bool working = true;
        ThreadPool.GetMaxThreads(out int maxWorkerThreads, out int maxCompletionPortThreads);
        while (working)
        {
            ThreadPool.GetAvailableThreads(out int workerThreads, out int completionPortThreads);
            //Console.WriteLine(
quot;{workerThreads} , {maxWorkerThreads}");
            if (workerThreads == maxWorkerThreads)
            { working = false; }
        }
        //when all threads are completed then 'working' will be false 
    }
    void xyz(object o)
    {
        console.writeline("");
    }
    static void Main(string[] args)
    {
        for (int i = 0; i < 10; i++)
        {
            ThreadPool.QueueUserWorkItem(new WaitCallback(xyz));
        }

        bool working = true;
        ThreadPool.GetMaxThreads(out int maxWorkerThreads, out int maxCompletionPortThreads);
        while (working)
        {
            ThreadPool.GetAvailableThreads(out int workerThreads, out int completionPortThreads);
            //Console.WriteLine(
quot;{workerThreads} , {maxWorkerThreads}");
            if (workerThreads == maxWorkerThreads)
            { working = false; }
        }
        //when all threads are completed then 'working' will be false 
    }
    void xyz(object o)
    {
        console.writeline("");
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文