This article is referenced from the C # parallel advanced programming
TPL supports data parallelism (with a large amount of data to process, must perform the same operations on each data, task parallelism (there are many operations that can run concurrently), pipelining (combination of task parallelism and data parallelism)
The new Task Parallel Library was introduced in. NET 4.0 to handle parallel development.
Parallel class
Keywords:
Parallel.For and Parallel.ForEach-load balancing multitasking
Parallel.Invoke-running multiple tasks in parallel
ParallelOptions-Specifies the maximum degree of parallelism (instantiates a class and modifies the value of the Maxdegreeofparallelism property)
Environment.processorcount-Maximum number of cores
Command-side task parallelism
Keywords: task class, a task class that represents an asynchronous operation (takes into account the cost of the task)
The start task uses the Start method of the task class, waits for the thread to finish using the WaitAll method, and interrupts the task's run through the CancellationTokenSource cancel method.
How do you express a parent-child relationship between tasks? Taskcreationoption's attachtoparent to finish.
How to express a serial task? Task.continuewith
Concurrent Collections
BlockingCollection
Concurrentdictionary/concurrentqueue/concurrentstack
Here is an example of a real C # multitasking concurrency.
Scenario: The main process opens a producer thread and a consumer thread. They can talk to each other, such as ([verb, noun]) Say,hello task,a. Producers say a word consumers listen, consumers either answer or submit new tasks or end themselves.
Code
Using system;using system.threading;using system.threading.tasks;using system.collections.concurrent;namespace Tpltest{class program{public static readonly int max_data_length = n; private static blockingcollection<mess age> bcstr = new Blockingcollection<message> (max_data_length);p ublic static readonly string say_thanks = "Thanks";p ublic static readonly string say_welcome = "welcome!"; public static readonly String BYE = "BYE";p ublic static readonly string SAY = "SAY";p UBL IC static readonly string task = "task";p ublic static readonly string timeout = "TIMEOUT";p UB LIC static readonly string online = "ONLINE";p ublic static readonly string what = ' what? '; public static readonly int WAIT = 20000;public static void Main (string[] args) {//consumer thread paralleloptions PO = new ParallelOptions ();p O. Maxdegreeofparallelism =-1; Parallel.Invoke (PO, () = = {int Selfid = Environment.currentmanagedthreadid; Message customer = new Message (); customer. Customerthreadid = Selfid; customer.content = ONLINE; Console.WriteLine (Customer. ToString (false)); while (true) {if (bcstr. TryTake (out customer, WAIT)) {customer. Customerthreadid = Selfid; Customer.doaction (); Console.WriteLine (""); Console.WriteLine (Customer. ToString (false)); if (Customer.endthread ()) {break; }} else {if (customer = = null) {customer = new Message ();} Customer. Customerthreadid = Selfid; Customer.content = TIMEOUT; Console.WriteLine (Customer. ToString (False));}}, () = {int prdid = Environment.currentmanagedthreadid; Message Productor = new Message (); Productor. Productorthreadid = Prdid; Productor.content = ONLINE; Console.WriteLine (Productor. ToString (True)), while (true) {Console.Write ("productor Behavior (i.e. Say,hello):"); string msgcontent = Console.ReadLine ();p roductor = new Message (); Productor. Productorthreadid =Prdid; Productor.key = Msgcontent.split (', ') [0];p roductor.content = Msgcontent.split (', ') [1]; Bcstr. ADD (Productor); if (Productor.endthread ()) {Break;}}});}} Class Message{public int Productorthreadid {get; set;} public int Customerthreadid {get; set;} public string key {get; set;} public string content{get; set;} public bool Endthread () {return string. Compare (key, program.bye) = = 0;} public string ToString (bool isproductor) {return string. Format ("{0} Thread ID {1}: {2}", Isproductor? "Productor": "Customer", Isproductor? Productorthreadid.tostring (): customerthreadid.tostring (), content);} public void DoAction () {if (string. Compare (key, program.say) = = 0) {content = string. Compare (content, program.say_thanks) = = 0? Program.SAY_WELCOME:Program.WHAT;} if (string. Compare (key, program.task) = = 0) {Task Taska = Task.Factory.StartNew (() = {Console.WriteLine ("Task A begin");Task Childoffatehra = Task.Factory.StartNew (() = {Console.WriteLine ("Sub Task A begin"); Thread.Sleep (1000); Console.WriteLine ("Sub Task A end"); }); Childoffatehra.wait (); Console.WriteLine ("Task A end"); }); Taska.continuewith (TASKB = {Console.WriteLine ("Task B begin"); Thread.Sleep (5000); Console.WriteLine ("Task B end"); }); taska.wait (); }}}}
C # Parallel Development summary