Parallel.ForEach 块吗? [英] Does Parallel.ForEach Block?

查看:40
本文介绍了Parallel.ForEach 块吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

.net 函数 Parallel.ForEach 是否会阻塞调用线程?我对行为的猜测是其中之一:

Does the .net function Parallel.ForEach block the calling thread? My guess as to the behavior is one of these:

  1. 是的,它会阻塞,直到执行最慢的项目返回.
  2. 不,它不会阻塞并立即返回控制权.要并行运行的项目在后台线程上完成.

或许还有其他事情正在发生,有人确定吗?

Or perhaps something else is happening, anyone know for sure?

在日志类中实现这个问题时出现了这个问题:

This question came up when implementing this in a logging class:

public class MultipleLoggingService : LoggingServiceBase
{
    private readonly List<LoggingServiceBase> loggingServices;

    public MultipleLoggingService(List<LoggingServiceBase> loggingServices)
    {
        this.loggingServices = loggingServices;
        LogLevelChanged += OnLogLevelChanged;
    }

    private void OnLogLevelChanged(object sender, LogLevelChangedArgs args)
    {
        loggingServices.ForEach(l => l.LogLevel = LogLevel);
    }

    public override LogMessageResponse LogMessage(LogMessageRequest request)
    {
        if (request.LogMessage)
            Parallel.ForEach(loggingServices, l => l.LogMessage(request));

        return new LogMessageResponse{MessageLogged = request.LogMessage};
    }
}

注意 LogMessage 方法调用其他一些日志服务.我需要那部分立即返回,所以它不会阻塞调用线程.

Notice the LogMessage method calls some other logging services. I need that part to return immediately, so it doesn't block the calling thread.

更新:基于其他人的评论(我们已确认行为是 #1).所以我建议使用 Task库并像这样重写循环:

Update: Based on comments from others (we have confirmed the behavior is #1). So I have taken advice to use the Task library and rewritten the loop like this:

          if (request.LogMessage)
            foreach (var loggingService in loggingServices)
                Task.Factory.StartNew(() => loggingService.LogMessage(request));

推荐答案

数字 1 是正确的;Parallel.ForEach 在循环完成之前不会返回.如果您不想要这种行为,您可以简单地将循环作为 Task 执行并在另一个线程上运行.

Number 1 is correct; Parallel.ForEach does not return until the loop has completed. If you don't want that behavior, you can simply execute your loop as a Task and run it on another thread.

这篇关于Parallel.ForEach 块吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆