Multi-threaded tasks

Source: Internet
Author: User

Multi-threaded tasks

Parallel class (http://www.cnblogs.com/afei-24/p/6904179.html) Parallel tasks need to end before the code to run, if you want to wait for the end before starting the action, you can use the Task class to better control the Parallel action.
A task represents a unit of work that should be completed. This work unit can run in a separate thread or start a task in synchronous mode, which requires waiting for the main thread. By using tasks, you can not only obtain an abstraction layer, but also control the underlying threads.
Compared with the Parallel class, tasks provide great flexibility. For example, you can define a continuous job-the job to be executed after a task is completed. This can be divided based on whether the task is successful or not. You can also arrange tasks in the hierarchy. For example, a parent task can create a new subtask.

I. Start the task
To Start a Task, you can use the TaskFactory class or the constructor of the Task class and the Start () method. The constructor of the Task class is flexible in creating tasks.
When starting a Task, an instance of the Task class is created. You can use the Action or Action <T> delegate (without a parameter or a parameter) to specify the code to be run.

  1. tasks using the thread pool
The thread pool provides a backend thread pool (the thread pool is described in detail later ). The thread pool manages threads independently, increasing or decreasing the number of threads in the thread pool as needed. The threads in the thread pool are used to implement some actions, and then return to the thread pool.

The following describes four methods to create a thread pool task:
First define a method to call:

// Avoid the intersection of write operations on the Console. Here, use the lock keyword to synchronize static object taskMethodLock = new object (); static void TaskMethod (object title) {lock (taskMethodLock) {Console. writeLine (title); Console. writeLine ("task id: {0}, thread: {1}", Task. currentId, Thread. currentThread. managedThreadId); Console. writeLine ("is pooled thread: {0}", Thread. currentThread. isThreadPoolThread); Console. writeLine ("is background thread: {0}", Thread. currentThread. isBackground );}}

(1) Use the instantiated TaskFactory class to pass the parameters of the TaskMethod method and TaskMethod method to the StartNew method:
Var tf = new TaskFactory ();
Task t1 = tf. StartNew (TaskMethod, "using a task factory ");

(2) Use the static attribute Factory of the Task class to access TaskFactory and call the StartNew () method. Similar to the first method, the factory is also used, but the control of the factory is not so comprehensive.
Task t2 = Task. Factory. StartNew (TaskMethod, "using factory via a task ");

(3) Use the constructor of the Task. The Task is not executed when the Task object is instantiated, but the Created state is specified. Call the Start () method to Start the task.
Task t3 = new Task (TaskMethod, "using a task constructor and Start ");
T3.Start ();

(4) directly call the Run () method of the Task class to start the Task. The Run () method does not pass the version with the parameter delegate. You can pass the lambda expression.
Task t4 = Task. Run () => TaskMethod ("using Run method "));

 

  static void Main(string[] args)        {            var tf = new TaskFactory();            Task t1 = tf.StartNew(TaskMethod,"using a task factory");            Task t2 = Task.Factory.StartNew(TaskMethod,"using factory via a task");            Task t3 = new Task(TaskMethod,"using a task constructor and Start");            t3.Start();            Task t4 = Task.Run(()=> TaskMethod("using Run method"));            Console.ReadKey();        }

 

  2. Synchronization task
Tasks do not necessarily use threads in the thread pool, but can also use other threads. The task can also run synchronously, with the same thread as the main thread.
Example:

      static void RunSyncTask()            {                TaskMethod("main thread");                var t = new Task(TaskMethod,"run sync");                t.RunSynchronously();            }

Output:
  

The code above calls the TaskMethod method directly on the main thread and then calls it on the created Task. The output shows that the main thread is a front-end thread without a task ID or a thread in the thread pool. When you call the RunSynchronously method, the same thread is used to create a task.

  3. Use a single-threaded task
Although the preceding tasks are not threads in the thread pool, they use the main thread, not independent, and cannot be asynchronous.
If the task code should be run for a long time, use TaskCreationOptions. LongRunning to tell the task scheduler to create a new separate thread instead of a thread in the thread pool. This thread can not be managed by the thread pool. When the thread comes from the thread pool, the task scheduler can decide to wait for the running task to complete and then use this thread instead of creating a new thread in the thread pool. For long-running threads, the task scheduler will immediately know that it is unwise to wait for them to finish. A new thread will be created.

Example:

      static void LongRunTask()            {                var t = new Task(TaskMethod,"long running",TaskCreationOptions.LongRunning);                t.Start();            }

Output:
  


Ii. Task result ---- Future
When the task ends, some useful status information can be written to the shared object. This shared object must be thread-safe. Another option is to use a task that returns a result. This type of task is also called future because it returns a result in the future. This requires a generic version of the Task class. You can use this class to define the type of results returned by a task.
Example:
If you use a generic Task <TResult>, TResult is the return type. The constructor passes the method to the Func delegate, and the second parameter is the delegate parameter.

      static void Main(string[] args)            {                var t = new Task<Tuple<int, int>>(TaskWithResult,Tuple.Create<int,int>(8,3));                t.Start();                Console.WriteLine(t.Result);                t.Wait();                Console.WriteLine("result from task:{0},{1}", t.Result.Item1, t.Result.Item2);                Console.ReadKey();            }

 

A method called by a task to return results can be declared as any type.

static Tuple<int, int> TaskWithResult(object o)        {            Tuple<int, int> div = (Tuple<int, int>)o;            int result = div.Item1 / div.Item2;            int reminder = div.Item1 % div.Item2;            Thread.Sleep(10000);            return Tuple.Create<int, int>(result,reminder);        }

The tuples (http://www.cnblogs.com/afei-24/p/6738155.html) are used here ).

3. Continuous tasks
You can specify that another task should be run after the task is completed. For example, if a new task that uses the result of the previous Task fails, the task should be cleaned up.
The Task processing program (the previous Task) does not contain parameters or an object parameter. The continuous processing program has a parameter of the Task type. Here, you can access the relevant information of the previous Task.
Example:

// When a task ends, multiple tasks can be started, and continuous tasks can also have another continuous task. Static void Main (string [] args) {Task t1 = new Task (DoFirst); Task t2 = t1.ContinueWith (DoSecond); Task t3 = t1.ContinueWith (DoSecond ); task t4 = t2.ContinueWith (DoSecond); t1.Start (); Console. readKey ();} static void DoFirst () {Console. writeLine ("do some task: {0}", Task. currentId); Thread. sleep (3000);} static void DoSecond (Task t) {Console. writeLine ("task {0} finished", t. id); Console. writeLine ("this task id: {0}", Task. currentId );}

 

 

 

No matter how the previous task ends, the previous continuous task is always started at the end of the previous task. You can use the value in TaskContinuationOptions to specify that a continuous task is started only when the task is successful or fails.
Task t5 = t1.ContinueWith (DoSecond, TaskContinuationOptions. OnlyOnFaulted );


Iv. Task hierarchy
With Task continuity, you can start another task after a task is completed. A task can also constitute a hierarchy. When a new task is enabled, a parent/child hierarchy is started. Canceling a parent task also cancels the subtask.
The code for creating a subtask is the same as that for creating a parent task. The only difference is that a subtask is created from another task.
Example:

static void Main(string[] args)        {            Task t = new Task(ParentTask);            t.Start();            Console.ReadKey();        }        static void ParentTask()        {            Console.WriteLine("parent task id:{0}",Task.CurrentId);            var child = new Task(ChildTask);            child.Start();            Console.WriteLine("parent  create child");        }        static void ChildTask()        {            Console.WriteLine("child task");                    }

If the parent task ends before the child task, the status of the parent task is WaitingForChildrenToComplete. When all sub-tasks are completed, the parent task is in the RanToCompletion state.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.