Thread & ConcurrentDictionary in C#
using System;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
public class Example
{
public static async Task Main()
{
string pattern = @"\p{P}*\s+";
string[] titles = { "Sister Carrie", "The Financier" };
Task<int>[] tasks = new Task<int>[titles.Length];
for (int ctr = 0; ctr < titles.Length; ctr++) {
string s = titles[ctr];
tasks[ctr] = new Task<int>( () => {
// Number of words.
int nWords = 0;
// Create filename from title.
string fn = s + ".txt";
StreamReader sr = new StreamReader(fn);
string input = sr.ReadToEndAsync().Result;
sr.Close();
nWords = Regex.Matches(input, pattern).Count;
return nWords;
} );
}
// Ensure files exist before launching tasks.
bool allExist = true;
foreach (var title in titles) {
string fn = title + ".txt";
if (! File.Exists(fn)) {
allExist = false;
Console.WriteLine("Cannot find '{0}'", fn);
break;
}
}
// Launch tasks
if (allExist) {
foreach (var t in tasks)
t.Start();
await Task.WhenAll(tasks);
Console.WriteLine("Word Counts:\n");
for (int ctr = 0; ctr < titles.Length; ctr++)
Console.WriteLine("{0}: {1,10:N0} words", titles[ctr], tasks[ctr].Result);
}
}
}
https://docs.microsoft.com/en-us/dotnet/api/system.threading.tasks.task-1.-ctor?f1url=https%3A%2F%2Fmsdn.microsoft.com%2Fquery%2Fdev15.query%3FappId%3DDev15IDEF1%26l%3DEN-US%26k%3Dk(System.Threading.Tasks.Task%601.%2523ctor);k(SolutionItemsProject);k(TargetFrameworkMoniker-.NETFramework,Version%3Dv4.6.1);k(DevLang-csharp)%26rd%3Dtrue&view=netframework-4.8
ConcurrentDictionary
public static ConcurrentDictionary<string, OnlineStatus> conCurrentCacheStatusUsers;
conCurrentCacheStatusUsers.AddOrUpdate(u.userid, u, (k, o) => u);
using System.IO;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
public class Example
{
public static async Task Main()
{
string pattern = @"\p{P}*\s+";
string[] titles = { "Sister Carrie", "The Financier" };
Task<int>[] tasks = new Task<int>[titles.Length];
for (int ctr = 0; ctr < titles.Length; ctr++) {
string s = titles[ctr];
tasks[ctr] = new Task<int>( () => {
// Number of words.
int nWords = 0;
// Create filename from title.
string fn = s + ".txt";
StreamReader sr = new StreamReader(fn);
string input = sr.ReadToEndAsync().Result;
sr.Close();
nWords = Regex.Matches(input, pattern).Count;
return nWords;
} );
}
// Ensure files exist before launching tasks.
bool allExist = true;
foreach (var title in titles) {
string fn = title + ".txt";
if (! File.Exists(fn)) {
allExist = false;
Console.WriteLine("Cannot find '{0}'", fn);
break;
}
}
// Launch tasks
if (allExist) {
foreach (var t in tasks)
t.Start();
await Task.WhenAll(tasks);
Console.WriteLine("Word Counts:\n");
for (int ctr = 0; ctr < titles.Length; ctr++)
Console.WriteLine("{0}: {1,10:N0} words", titles[ctr], tasks[ctr].Result);
}
}
}
ConcurrentDictionary
public static ConcurrentDictionary<string, OnlineStatus> conCurrentCacheStatusUsers;
conCurrentCacheStatusUsers.AddOrUpdate(u.userid, u, (k, o) => u);
评论
发表评论