Program Ⅰ: Create a new thread from the task class
Using system;using system.collections.generic;using system.linq;using system.text;using system.threading;using system.threading.tasks;namespace test000{ class Program { /// <summary> /// task Class Package /// </summary> class MyTask { / <summary> /// First task class, count per second, Count 5 times /// </ summary> public static VOID&NBSP;T1 () &NBSP;&NBSP;&NBSP;&NBsp; { console.writeline ("task #{0}: begin!", Task.CurrentId ); for ( int i = 0; i < 5; i++) { thread.sleep (; ) console.writeline (" Task #{0}: {1} ", task.currentid, i); } &nbsP; console.writeline ("task #{0}: terminated!", task.currentid); } /// <summary> // / second task class, count per second, Count 5 times /// < /summary> public static Void t2 () { console.writeline ("Task #{0 }: begin! ", task.currentid); thread.sleep (+); for (int i = 0; i < 5; i++) { thread.sleep (; ) Console.WriteLine ("Task #{0}: {1}", task.currentid, i); } console.writeline ("task #{0}: terminated!", task.currentid); } } static void Main (String[] args) &NBSP;&Nbsp; { //set up two task task tsk1 = new task (MYTASK.T1); Console.WriteLine ("Task #{0}: constructed!", tsk1. ID); task tsk2 = new task (MYTASK.T2); Console.WriteLine ("Task #{0}: constructed!", tsk1. ID); //Run task tsk1. Start (); tsk2. Start (); //wait for task to run end &Nbsp; waitall (TSK1,&NBSP;TSK2); console.readline (); } /// <summary> /// wait for all task run to end /// </summary > /// <param name= "Tsks" > Waiting task Class </param > public static void waitall (Params Task [] tsks) { foreach (var t in tsks) { T.wait (); } } }}
Run results
Program Ⅱ: Start a task by TaskFactory and receive the return value of the task
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks;namespace Test001{ class Program { /// <summary> /// task Class Package /// </summary > class MyTask { /// <summary> /// for 1+2+...+n and /// </summary> /// <param name= "n" > Number n</param> /// <returns></returns> public static int sum (object n) { int x = (int) n; int sum = 0; for (int i = 1; i <= x; i++) { sum += i; } return sum; } } static void Main (String[] args) { task<int> tsk = task<int>. Factory.startnew (mytask.sum, 100); console.writeline ("result is: " + tsk. Result); console.readline (); } }}
Run results
Program Ⅲ: Call multiple tasks in parallel through the invoke function of the parallel class
Creating a new task with a lambda expression in this program
Using system;using system.collections.generic;using system.linq;using system.text;using system.threading;using system.threading.tasks;namespace test002{ class Program { static void Main (String[] args) { Parallel.Invoke ( () => { console.writeline ("Task #{0}: begin! ", task.currentid); for (int i = 0; i < 5; i++) { thread.sleep (; ) Console.WriteLine ("Task #{0}: {1}", task.currentid, i); } console.writeline ("task #{0}: terminated!", task.currentid); }, () => { console.writeline ("Task #{0}: begin! ", task.currentid); thread.sleep (+); for ( int i = 0; i < 5; i++) { thread.sleep (; ) console.writeline ("Task #{0}: {1}", task.currentid, i); } &nbsP; console.writeline ("task #{0}: terminated!", Task.CurrentId); } ) ; console.readline (); } }}
Run results
Program Ⅳ: multiple tasks are called in parallel by the for and Foeeach functions of the parallel class
In this procedure, the elapsed time of the Stopwatch class statistic program segment
As you can see from the example, not all loops are valid for parallelization. In general, using sequential loops is faster than parallel loops for small loops or loops that perform very simple operations
using system;using system.collections.generic;using system.diagnostics;using system.linq; Using system.text;using system.threading.tasks;namespace test003{ class Program { /// <summary> /// Example Function / </summary> /// <param name= "n" > Parameters </param> public static void dosomething ( Int n) { int sum = 0; for (int i = 0; i < n * 100; i++) { sum += i; } } static void main (String[] args) { //timing tool, requires system.diagnostics stopwatch sw = new stopwatch () //statistics The time that the function was called sequentially &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SW. Start (); for (int i = 100; i < 105; i++) { dosomething (; ) &NBSP;}&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SW. Stop (); console.writeline ("TotalTime: {0} ", &NBSP;SW. elapsed.totalmilliseconds); sw. Reset (); console.writeline ("=========== "); //statistics time of parallel call function &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SW. Start (); parallel.for (100, 105, dosomething); sW.stop (); console.writeline ("TotalTime: {0} ", &NBSP;SW. elapsed.totalmilliseconds); sw. Reset (); console.writeline ("=========== "); //statistics time of parallel call function &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SW. Start (); parallel.foreach (new int[ 5] { 100, 101, 102, 103, 104 }, dosomething); &NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;&NBSP;SW. Stop (); console.writeline ("TotalTime: {0} ", &NBSP;SW. Elapsed.totalmilliseconds); &nbsP;SW. Reset (); console.readline (); } }}
Run results
END
C # TPL Learning (4 programs)