Before moving to asynchronous processing, we will give a small focus on synchronous processing.
Synchronous Process
It is a two way communicative process or we can say a request/response process.This process results the output/response immediately like as we talk on telephone/mobile and getting response instantly.
Asynchronous process
It is two way communicative processes. Here result can be obtained immediately or not instantly. The result can be returned back to the caller/source by callback process.
Asynchronous process can be implemented in different way or method
1. Thread
2. Thread Pool
3. The task based
4. The event based
5. The IASyncResult
Synchronous Process
It is a two way communicative process or we can say a request/response process.This process results the output/response immediately like as we talk on telephone/mobile and getting response instantly.
Asynchronous process
It is two way communicative processes. Here result can be obtained immediately or not instantly. The result can be returned back to the caller/source by callback process.
Asynchronous process can be implemented in different way or method
1. Thread
2. Thread Pool
3. The task based
4. The event based
5. The IASyncResult
Thread
It is a mechanism that allows working only one thread at a time. If more than one thread has to be executing at same time then it would force to happen data corruption or data damage.Suppose we have more than one thread and every thread has to be executed. In this case we can have loss of time because of thread mechanism and its one thread at a time paradigm. One observation says that one thread takes about 2 minutes to executes itself.
If we are executing one thread, the operating system has to perform many things:
1. Additional data structure
2. Thread environment block
3. User and kernel mode stack
4. Thread context switching
1. Additional data structure
2. Thread environment block
3. User and kernel mode stack
4. Thread context switching
To perform all above actions, operating system takes much time (about 2 minutes).This reduces performance of process. So we can avoid it when we have many process to execute to acquire the specific performance
Thread Pool
When we do want to perform asynchronous operation,Thread Pooling is one of the good option depending on your purpose.It is used to perform several tasks in the background.
Thread Pool is managed by CLR.Whenever the process has to be executed,just do the queue the task in the thread pool without worrying about how the task will execute.Each task from the thread pool is assigned to a thread.After completing the task thread return to the thread waiting queue and takes other task to execute.Thread pool have a number of thread and if all thread are busy in performing their task then incoming request used to keep in waiting queue.
To use thread pool,we have a namespace in .net named as System.Threading.ThreadPool where QueueUserWorkItem method which is static in nature used to take control of waiting task in queue.
It is certain that the task will execute if it is in thread pool but uncertain when will it execute.So take care while using thread pool keeping in mind your purpose.
public void CompleteTask()
{
// Queue a first task.
System.Threading.ThreadPool.QueueUserWorkItem(
new System.Threading.WaitCallback(FirstTask));
// Queue second task.
System.Threading.ThreadPool.QueueUserWorkItem(
new System.Threading.WaitCallback(SecondTask));
}
private void FirstTask(Object state)
{
// Insert code to perform a first task.
}
private void SecondTask(Object state)
{
// Insert code to perform a second task.
}
The Task Based
The .net framework 4.0 introduces Task Parallel Library (TPL) for parallel computing and asynchronous processing in System.Threading and System.Threading.Tasks namespaces.TPL dynamically used the available thread in Thread Pool. The parallelism is the concept of running all the process in parallel.
To use the task based asynchronous processing, we create the instance of Task class and then call the Task’s method Start () to schedule and start the execution of a task available in Thread Pool.
Task t=new Task (Delegate);
t.Start ();
We can use delegate as parameter in constructor of Task class.
Here two things can happen
1. We can block the current thread and wait till specified task completes its execution and in this case we can use Task’s method Task.WaitAll (t).
2. We will not block the current thread and will do some process after some specific task completes. In this case we can use Task’s method ContinueWith().
After completion of execution of specific task, we can get the result by Task’s property called as Result and can use t.Result
Creation of a new Task
var t=new Task(FirstTask);
t.start();
Waiting on Tasks
Task t1=Task.Factory.StartNew(FirstTask);
Task t2=Task.Factory.StartNew(SecondTask);
Task t3=Task.Factory.StartNew(ThirdTask);
Task.WaitAll(t1,t2,t3)
Execute another Async task
Task.Factory.StartNew(fourthWork).ContinueWith(AsynchTask)
Task t1=Task.Factory.StartNew(AsyncTask);
The advantage of TPL
1.To create parent to child tasks and create complex task hierarchy of dependent tasks.
2.To Cancel or timeout tasks.
3.To Wait for one or all tasks.
4.The Continuation options for failed and succeeded tasks.
5.To catch and handle exceptions.
Thread Pool
When we do want to perform asynchronous operation,Thread Pooling is one of the good option depending on your purpose.It is used to perform several tasks in the background.
Thread Pool is managed by CLR.Whenever the process has to be executed,just do the queue the task in the thread pool without worrying about how the task will execute.Each task from the thread pool is assigned to a thread.After completing the task thread return to the thread waiting queue and takes other task to execute.Thread pool have a number of thread and if all thread are busy in performing their task then incoming request used to keep in waiting queue.
To use thread pool,we have a namespace in .net named as System.Threading.ThreadPool where QueueUserWorkItem method which is static in nature used to take control of waiting task in queue.
It is certain that the task will execute if it is in thread pool but uncertain when will it execute.So take care while using thread pool keeping in mind your purpose.
public void CompleteTask()
{
// Queue a first task.
System.Threading.ThreadPool.QueueUserWorkItem(
new System.Threading.WaitCallback(FirstTask));
// Queue second task.
System.Threading.ThreadPool.QueueUserWorkItem(
new System.Threading.WaitCallback(SecondTask));
}
private void FirstTask(Object state)
{
// Insert code to perform a first task.
}
private void SecondTask(Object state)
{
// Insert code to perform a second task.
}
The Task Based
The .net framework 4.0 introduces Task Parallel Library (TPL) for parallel computing and asynchronous processing in System.Threading and System.Threading.Tasks namespaces.TPL dynamically used the available thread in Thread Pool. The parallelism is the concept of running all the process in parallel.
To use the task based asynchronous processing, we create the instance of Task class and then call the Task’s method Start () to schedule and start the execution of a task available in Thread Pool.
Task
t.Start ();
We can use delegate as parameter in constructor of Task class.
Here two things can happen
1. We can block the current thread and wait till specified task completes its execution and in this case we can use Task’s method Task.WaitAll (t).
2. We will not block the current thread and will do some process after some specific task completes. In this case we can use Task’s method ContinueWith().
After completion of execution of specific task, we can get the result by Task’s property called as Result and can use t.Result
Creation of a new Task
var t=new Task(FirstTask);
t.start();
Waiting on Tasks
Task t1=Task.Factory.StartNew(FirstTask);
Task t2=Task.Factory.StartNew(SecondTask);
Task t3=Task.Factory.StartNew(ThirdTask);
Task.WaitAll(t1,t2,t3)
Execute another Async task
Task.Factory.StartNew(fourthWork).ContinueWith(AsynchTask)
Task t1=Task.Factory.StartNew(AsyncTask);
The advantage of TPL
1.To create parent to child tasks and create complex task hierarchy of dependent tasks.
2.To Cancel or timeout tasks.
3.To Wait for one or all tasks.
4.The Continuation options for failed and succeeded tasks.
5.To catch and handle exceptions.
No comments:
Post a Comment