1. In. net4.0, there is a new class library: Task Parallel Library. It greatly simplifies parallel programming and is rich in content. Here are just some of the simplest
Parallel.For loops and Parallel.ForEach loops. They are located in the System.Threading.Tasks namespace. They are two methods that put iterations in parallel on different processors if the machine is a multiprocessor or multi-core processor, which can greatly improve performance.
2. Examples are calculated using Parallel.For, assigning and printing the array, and using Parallel.ForEach to calculate the length of each element of the string arrays, running the result:
3. Code:
using System;using System.Threading.Tasks; You must use this namespace namespace multithreaded methods for multicore processors Parallel.For and parallel.foreach{class program {static void Main (string[] args) {Parallel.For (0,5,i=>console.writeline ("The square of {0} is {1}", I, i*i)); Console.WriteLine (); const int maxvalues = 5; Int[] Squares=new int[maxvalues]; Parallel.For (0, Maxvalues,i=>console.writeline (i*i)); Console.WriteLine (); string[] Squarsstr = new string[]{"We", "is", "the", "Kings", "of", "This", "beautiful", "World" , "How", "Does", "You", "think"}; Parallel.ForEach (squarsstr, I = Console.WriteLine (string. Format ("{0} has {1} letters", i,i.length)); Console.WriteLine (); } }}
Multithreaded methods for multi-core processors for C # multithreading Parallel.For and Parallel.ForEach