Anonymous user
Concurrent computing: Difference between revisions
m
Updated whitespace.
(Removed external library solution, as task can be done through standard ones.) |
m (Updated whitespace.) |
||
Line 166:
=={{header|c sharp|C#}}==
===With Threads===
<lang csharp>static Random tRand = new Random();
static
▲ Thread t = new Thread(new ParameterizedThreadStart(WriteText));
▲ t.Start("Enjoy");
▲ t = new Thread(new ParameterizedThreadStart(WriteText));
▲ t.Start("Rosetta");
▲ t = new Thread(new ParameterizedThreadStart(WriteText));
▲ t.Start("Code");
▲ Console.ReadLine();
}
private static void WriteText(object p) {▼
▲private static void WriteText(object p)
}</lang>▼
▲ Thread.Sleep(tRand.Next(1000, 4000));
▲ Console.WriteLine(p);
▲</lang>
===With Tasks===
{{works with|C sharp|7.1}}
<lang csharp>using System;
using System.Threading.Tasks;
public class Program {▼
▲public class Program
static async Task Main() {
Task t1 = Task.Run(() => Console.WriteLine("Enjoy"));
Task t2 = Task.Run(() => Console.WriteLine("Rosetta"));
Task t3 = Task.Run(() => Console.WriteLine("Code"));
await Task.WhenAll(t1, t2, t3);
}
Line 214 ⟶ 195:
<lang csharp>using System;
using System.Threading.Tasks;
public class Program {▼
▲public class Program
static void Main() => Parallel.ForEach(new[] {"Enjoy", "Rosetta", "Code"}, s => Console.WriteLine(s));
}</lang>
|