Parallel Class
The parallel class defines a static method for, foreach, and Invoke. The parallel class uses multiple tasks, so multiple threads are used to complete the job.
Parallel.For
The Parallel.For () method is similar to the FOR Loop statement for C #, and it is a task that executes multiple times. Using the Parallel.For method, you can run iterations in parallel. The order of the iterations is undefined .
Parallel.For (0, i + ={ Console.WriteLine ("idx:{0}, Task:{1}, THREAD:{2}", I, Task.currentid, Thread.CurrentThread.ManagedThreadId);}); //From the results can be seen, the order is not guaranteed.
You can also interrupt the for method prematurely. An overloaded version of the for () method accepts parameters of the third action<int, parallelloopstate> type. Using these parameters to define a method, you can call the break () or stop () method of parallelloopstate to affect the result of the loop.
- Break (), the loop should stop executing iterations outside the current iteration at the earliest convenience of the system.
- Stop (), the loop should stop executing at the earliest convenience of the system.
Parallelloopresult result =Parallel.For (0,Ten, (intI, parallelloopstate pls) ={Console.WriteLine ("idx:{0}, Task:{1}, thread:{2}", I, Task.currentid, Thread.CurrentThread.ManagedThreadId); Thread.Sleep (Ten); if(I >5) pls. Break (); //Stop as soon as possible, and no immediate stop is achieved }); Console.WriteLine ("result. iscompleted {0}", result. iscompleted); Console.WriteLine ("result. lowestbreakiteration {0}", result. Lowestbreakiteration);
The Parallel.For method may use several threads to perform loops, and if you need to initialize each thread, you can use the Parallel.for<tlocal> () method. In addition to the values from and to, three delegate parameters are accepted.
- The first parameter type is FUNC<TLOCAL>, and the method is called only once per thread.
- The second parameter defines the delegate for the loop body, the first parameter of the delegate is the loop iteration, the second parameter parallelloopstate allows the stop iteration (as in the previous example), and the third parameter is the value returned from the Init method.
- The third parameter specifies a delegate action<tlocal>, and the method is called only once per thread.
parallel.for<string> (0,Ten, () ={Console.WriteLine ("---init thread {0}, task{1}", Thread.CurrentThread.ManagedThreadId, Task.currentid); return string. Format ("t{0}", Thread.CurrentThread.ManagedThreadId);}, (i, pls, str1)={Console.WriteLine ("!!! Body I {0} str1 {1} thread {2} task {3}", I, str1, Thread.CurrentThread.ManagedThreadId, Task.currentid); Thread.Sleep (Ten); return string. Format ("I {0}", i);}, (STR1)={Console.WriteLine ("@@ ZZFCTHOTFIXZ {0}", str1);});
Parallel.ForEach
The Parallel.ForEach method traversal implements the collection of IEnumerable, in a manner similar to a foreach statement but traversed asynchronously. There is also no definite traversal order here.
string[] arr = {"name","KKK","III","yyy","zzz","111" }; Parallel.ForEach<string> (arr, value = =Console.WriteLine (value)); Parallel.ForEach<string> (arr, (value, pls, k) = ={Console.WriteLine ("{0}, {1}", value, K); if(Value = ="KKK") pls. Stop ();});
Parallel.Invoke
If multiple tasks should run in parallel, you can use the Parallel.Invoke method.
Public Static void Invoke (params action[] actions); Public Static void params Action[] actions);
C # Multithreading series (iv)