Multithreading is an effective method to improve the efficiency of the program. Of course, for efficiency, more resources such as CPU, memory and so on need to be used.
Concurrency is two queues alternately using a coffee machine, parallel is two queues using two coffee machines at the same time , if serial, a queue using a coffee machine, then even if the person in front of constipation to go to the toilet for half a day, the back of the people can only death him back to pick up coffee, This efficiency is undoubtedly the lowest.
Both concurrency and parallelism can be a lot of threads, just to see if these threads can be executed by (multiple) CPUs at the same time, if it is possible to be parallel, and concurrency is a number of threads that are switched on and off by (a) CPU.
Run a thread
1 class Program2 {3 Static voidMain (string[] args)4 {5 NewThread (Go). Start ();//. NET 1.0 has started.6Task.Factory.StartNew (Go);//. NET 4.0 introduces the TPL7Task.run (NewAction (Go));//. NET 4.5 Adds a new run method8Console.WriteLine ("I am the main thread {0}", Thread.CurrentThread.ManagedThreadId.ToString ());9 varDayname = task.run<string> (() = {returnGetdayofthisweek ();});TenConsole.WriteLine ("today is: {0}", Dayname.result); One console.readline (); A - } - Public Static voidGo () the { -Console.WriteLine ("I'm another thread {0}", Thread.CurrentThread.ManagedThreadId.ToString ()); - } - + Public Static stringGetdayofthisweek () - { + return string. Format ("result {0}", Thread.CurrentThread.ManagedThreadId.ToString ()); A } at}
TaskFactory class
- Create a task and start the StartNew method immediately by calling
Task wait
-
c Ontinuewhenany method. " > Create a task in one of the tasks in the array by calling finish after the start of the ContinueWhenAny method.
-
continuewhenall method. " > Create a task in which all the tasks in the array have been completed by calling the start ContinueWhenAll method.
|
AsyncState |
Gets the state object provided when the Task was created, or null if not provided. |
|
Completedtask |
Gets a task that has completed successfully. |
|
Creationoptions |
Gets the taskcreationoptions used to create this task. |
|
CurrentID |
Returns the ID of the currently executing Task. |
| |
exception |
task to end prematurely. " > get the task that led to AggregateException early termination. task completed successfully or have not yet thrown no exceptions, this would return null ." > if task completed successfully or has not thrown any exceptions, this will return null . |
|
Factory |
Provides access to the factory method used to create task and task<TResult> . |
|
Id |
Gets The ID of this Task instance. |
|
IsCanceled |
Gets Whether this Task instance has been executed because of the reason it was canceled. |
|
IsCompleted |
Gets Whether this Task has completed. |
|
isfaulted |
Gets whether the Task was completed because of an unhandled exception. |
|
Status |
Gets the taskstatus for this task. |
Https://msdn.microsoft.com/zh-cn/library/system.threading.tasks.task (v=vs.110). aspx
Of course, this does not take into account the operation of shared data, when multiple threads operate on the same data source, the uncontrolled order can have unintended consequences.
. Net multithreaded (1) Task