■ Time points related to the task class
The task class can be used to execute multiple processes. At this time, there are generally two time points you want to know. One is the end time of a single task. Another is the end time of all tasks. These two time points can be easily identified.
Parameters of the task. waitany and task. waitall methods are one or more task objects. The two methods return the end time of a single task and the end time of all tasks. Using these two methods, you can easily write code to create multiple tasks and wait for their end time.
Task3.cs
using System; using System.Threading.Tasks; class Program { static void Main(string[] args) { var task1 = Task.Factory.StartNew(() => { for (int i = 0; i < 100; i++) Console.Write('A'); }); var task2 = Task.Factory.StartNew(() => { for (int i = 0; i < 200; i++) Console.Write('B'); }); var task3 = Task.Factory.StartNew(() => { for (int i = 0; i < 300; i++) Console.Write('C'); }); Task.WaitAny(task1, task2, task3); Console.WriteLine(); Console.WriteLine("stopped one task"); Task.WaitAll(task1, task2, task3); Console.WriteLine(); Console.WriteLine("stopped all tasks"); } } |
Running result (the result varies with the environment and time series)
ABBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBBBBAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA stopped one task CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB BBBBBBBBBBBBBBB stopped all task |
So there is a problem here. Although the task. waitany method and the task. waitall method are easy to use, the control function for the relationship between tasks is not as powerful as the control function for the relationship between threads. No matter how simple it becomes, the synchronization problem still exists completely. To compete for resources that are being used by other classes, the lock statement must be used after all. So, should I use the task?
The answer is "should be used ". "Accessibility and ease of use" is a feature of the task class. Using the task class is one of the corresponding methods for the multi-core era. Although there are still synchronization problems, the multi-core era has become a non-problem or negligible problem. On the contrary, it is important to develop the habit of dividing parallel processing into several tasks for processing. This is a way to make full use of the power of the future multi-core CPU.
■ Plinq (parallel
LINQ)
Although Plinq is a hot topic about parallel processing, its usage is very simple.
The following example shows the simple code for creating a query expression. The program itself does not make much sense, except to find "2" and then output it.
Create a simple code containing a query expression
using System; using System.Linq; using System.Threading.Tasks; class Program { static void Main(string[] args) { int[] ar = { 1, 2, 3 }; var q1 = from n in ar where n == 2 select n; foreach (var n in q1) Console.WriteLine("found {0}", n); } } |
Running result
If you change the preceding example to parallel processing, you only need to append the asparallel Method to the query expression.
Change the previous example to use parallel processing.
var q1 = from n in ar.AsParallel() where n == 2 select n; |
The method format is the same. For example, the following query expression.
Method-based query expression
var q1 = ar.Where((c) => c == 2); |
As in the following example, insert the asparallel method at the beginning of the method chain.
Change the previous example to use parallel processing.
var q1 = ar.AsParallel().Where((c) => c == 2); |
From the above two examples, we can know that Plinq is very simple. As long as a method is used, we can use parallel processing to process the query expression. However, if we say this will bring performance benefits, it is not necessarily. Because, there are the following situations.
1. When other servers use Query expressions for queries, using Plinq may not necessarily bring much benefit to performance (because it is not in the scope of local parallel processing. Plinq is mainly used for local-end LINQ
Processing of to objects is not necessarily effective for processing of LINQ to SQL)
2. when a large number of Query expressions are used, not every query expression is the key to performance bottleneck. If each query expression is inserted with the asparallel method, it will not bring much benefit, while wasting time, the Code's readability is also reduced.
3. After the asparallel method is inserted, the result sometimes changes.
The important question is the last one. What changes will happen? Next page.