CSharp · Console
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
public class Program
{
public static void Main()
{
string[] documents = new[] { 'n', 'a', 's', 'j', 'a', 'l', 'v', 'g', 'a', 'f', 'f' }
.Select((item, index) => $"{item}-{index}")
.ToArray();
Console.WriteLine($"Documents: [{String.Join(", ", documents)}]");
var grouped = documents.GroupBy(item => item[0]); // Group by the first char
ParallelOptions options = new()
{
MaxDegreeOfParallelism = Environment.ProcessorCount
};
Parallel.ForEach(grouped, options, grouping =>
{
foreach (var document in grouping)
{
Console.WriteLine($"Processing: {document}");
Thread.Sleep(500);
}
});
}
}