【发布时间】:2016-04-22 19:42:30
【问题描述】:
我需要并行运行多个异步方法并等待所有这些方法。 (Task.WhenAll) 然后在这些方法中,我需要访问一个共享资源,如 Dictionary()。我需要它是线程安全的吗?
我尝试运行以下两个示例,两者似乎都暗示它是多线程的(交错的消息/日期)。 http://rextester.com/AEH56431http://rextester.com/YEB50034
这张图有问题吗?任何人都可以确认/否认? 也没有看到 C# 并发大师 Stephen Cleary 专门谈论这个案例。 编辑:实际上 Stephen 在他的 async intro blog 中谈到了线程模型,这可以提供帮助。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
namespace Rextester
{
public class Program
{
static public async Task run2()
{
Console.WriteLine("START");
await Task.WhenAll(steps(1),steps(2),steps(3),steps(4));
Console.WriteLine("STOP");
}
static public async Task steps(int i) {
await Task.Delay(100*i);
Console.WriteLine("step"+i+".1");
Thread.Sleep((5-i)*300);
Console.WriteLine("step"+i+".2");
Thread.Sleep((5-i)*300);
Console.WriteLine("step"+i+".3");
Thread.Sleep((5-i)*300);
Console.WriteLine("step"+i+".4");
}
public static void Main(string[] args)
{
run2().Wait();
}
}
}
结果是:
START
step1.1
step2.1
step3.1
step4.1
step4.2
step3.2
step4.3 <-- multithreading? (2 isn't done)
step2.2
step1.2
step4.4
step3.3 <-- multithreading?
step2.3
step3.4
step1.3 <-- multithreading?
step2.4
step1.4
STOP
然后是另一个变体:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Text;
namespace Rextester
{
public class Program
{
static public async Task run()
{
Console.WriteLine("START");
await Task.WhenAll(steps(1),steps(2),steps(3),steps(4));
Console.WriteLine("\nSTOP");
}
static public async Task steps(int i) {
var a = new StringBuilder();
await Task.Delay(100*i); // This is to force to run in Async mode.
// following is a block of code with (hopefully) no "async waits". Just CPU-bound (Thread.Sleep should be blocking the thread I think?)
a.Append("\nSTART ["+Thread.CurrentThread.ManagedThreadId+"]");
a.Append("\nstep "+i+".1 >"+DateTime.Now.ToString("mm:ss.fff"));
Thread.Sleep((5-i)*400);
a.Append("\nstep "+i+".2 >"+DateTime.Now.ToString("mm:ss.fff"));
Thread.Sleep((5-i)*400);
a.Append("\nstep "+i+".3 >"+DateTime.Now.ToString("mm:ss.fff"));
Thread.Sleep((5-i)*400);
a.Append("\nstep "+i+".4 >"+DateTime.Now.ToString("mm:ss.fff"));
a.Append("\nSTOP");
Console.WriteLine(a.ToString());
}
public static void Main(string[] args)
{
run().Wait();
}
}
}
输出如下:
START
START [9]
step 4.1 >57:08.485
step 4.2 >57:08.891
step 4.3 >57:09.298
step 4.4 >57:09.704
STOP
START [8]
step 3.1 >57:08.391
step 3.2 >57:09.204
step 3.3 >57:10.017
step 3.4 >57:10.830
STOP
START [7]
step 2.1 >57:08.297
step 2.2 >57:09.501
step 2.3 >57:10.705
step 2.4 >57:11.908
STOP
START [6]
step 1.1 >57:08.203
step 1.2 >57:09.814
step 1.3 >57:11.424
step 1.4 >57:13.034
STOP
STOP
【问题讨论】:
-
如果你想展示一些东西,打印
CurrentThread.ManagedId。 -
okidoki。我找到了 Thread.CurrentThread.ManagedThreadId 但没有找到 ManagedId。都有不同的 ManagedThreadId
-
只是在此处说明问题已发生重大变化。查看编辑历史记录。
-
上一个问题是:“任务可以从线程中产生并行运行吗?”我试图通过谈论典型的多线程问题来更加“具体”。对我来说这是同一个问题!对困惑感到抱歉。无论如何我接受了你的回答。谢谢!
-
您的第二个版本没有抓住重点,因为您的所有线程仍然是 Task.Delay 的副作用。对于任何真实事物,这根本不是一个好的模型。
标签: c# .net multithreading concurrency task