Manage Threading in C#


What is a process ?

process is an instance of a computer program that is being executed. It contains the program code and its activity. Depending on the operating system (OS), a process may be made up of multiple threads of execution that execute instructions concurrently.
In current version of Windows each application runs in its own process with its own virtual memory ,So it isolates from other applications from affecting.

What is a thread ?

 A thread is a component of a process. Multiple threads can exist within one process, executing concurrently and sharing resources such as memory, while different processes do not share these resources.

What is context switching ?

A context switching is the process of storing the state of a process or of a thread, so that it can be restored and execution resumed from the same point later.

How Thread Class works ?

namespace : System. Threading
Using Thread class , we could have all the configuration of a thread.
Thread t  = new Thread(new ThreadStart(Method));
t.Start();
t.Join()
The Join() method is called on the main thread to let it wait until this thread finishes.
Thread.Sleep(0) signals OS that the thread execution is finished.
t.IsBackground = true (background threads exits immediately)
t.IsBackground = false(foreground threads keeps the application alive)

t.Start(5) - To pass a parameter in Start method ,ParameterizedThreadStart() should be used instead of ThreadStart()
Thread.Abort() - Stops a thread by the main thread
* Better way to stop a thread is using a shared variable , so that main thread can change its status and the target thread check the variable status and stop or continue.

What are local and shared variable in Multithreading ? 

[ThreadStatic] attribute makes a thread own variable but not a local variable. Each thread has its own copy of this .

ThreadLocal<T> class takes a delegate and can be initialized inside thread as a local variable.

What is execution context of a Thread ?

Thread.CurrentThread

What are Thread pooling ?

Thread pool are created to reuse threads. It is a collection of worker threads.
The thread pool is primarily used to reduce the number of application threads and provide management of the worker threads. A Task is a object that represents some work that needs to be done.(Thread pool is like Uber app and Task is like Passenger ,worker thread is like Uber Cab)
Wait() in Task is equivalent to Join() in Thread.
Adding continuation task :

How Task Class work ?

A Task object typically executes asynchronously on a thread pool thread rather than synchronously on the main application thread, you can use the Status property, as well as the IsCanceled, IsCompleted, and IsFaulted properties, to determine the state of a task.

How Task.Result returns the value ?

When a thread is attempting to read the Result of another Task ,if the task is not finished then the current thread will be blocked.

What is difference in Continuation task and Child task ?

We can add a continuation method to execute some operations after task is finished using ContinueWith(lamda).
 TaskContinuationOption are used for some special cases .
OnlyOnCanceled  : only if its antecedent (predecessor) task was cancelled.
OnlyOnFaulted : antecedent threw an unhandled exception. (opp. NotOnFaulted )
**This option is not valid for multi-task continuations.
OnlyOnRanToCompletion : only if its antecedent ran to completion. (opp. NotOnRanToCompletion )
RunContinuationsAsynchronously : continuation task should be run asynchronously.
ExecuteSynchronously : continuation task should be executed synchronously. With this option specified, the continuation runs on the same thread that causes the antecedent task to transition into its final state. If the antecedent is already complete when the continuation is created, the continuation will run on the thread that creates the continuation.


AttachedToParent :
Specifies that the continuation, if it is a child task, is attached to a parent in the task hierarchy.
1. The continuation can be a child task only if its antecedent is also a child task.
2. By default, a child task (that is, an inner task created by an outer task) executes independently of its parent.
3. You can use the AttachedToParent option so that the parent and child tasks are synchronized.
4. Note that if a parent task is configured with the DenyChildAttach option, the AttachedToParent option in the child task has no effect, and the child task will execute as a detached child task.
 
Secondly, A parent Task finishes when all its child task are ready .We can create parent task as array Task<int[]> and return a Task int[] inside Task.Run() and using TaskCreationOption.AttachedToParent .

A TaskFactory is used to reuse same creation and continuation configurations for creating child tasks.
Task.WaitAll() waits for all the child tasks to finish and Task.WaitAny() waits for one of the child task to finish and returns the position of the task in the list.

What is Parallel class ?

Parallel class can be used to achieve parallel execution in For ,ForEach and Invoke.

What is Async and Await ?

The keyword async is used to mark a method for asynchronous operations . The compiler transform this code into state machine. These allows the method to be split into multiple pieces with await keyword.

What is the use of ConfigureAwait(false) ?

Ans

How PLINQ works ?

Ans

What are concurrent collection in .NET ?

Ans

 





 

Comments