• Register
1,160 points
8 6 3

1. What is the difference between threads and tasks?

Tasks are wrapper around Thread and ThreadPool classes. Below are some major differences between Threads and Tasks:

A Task can return a result but there is no proper way to return a result from Thread.

We can apply chaining on multiple tasks but we can not in threads.

We can wait on Tasks without using Signalling. But in Threads we have to use event signals like AutoResetEvent and ManualResetEvent.

We can apply Parent/Child relationship in Tasks. A Task at one time becomes parent of multiple tasks. Parent Task does not complete until it's child tasks are completed. We do not have any such mechanim in Thread class.

Child Tasks can propagate their exceptions to to parent Task and All child exceptions are available in AggregateException class.

Tasks has in-build cancellation mechanism using CancellationToken class.

2. What is the difference between process and thread?

A process is started when you start an Application. The process is a collection of resources like virtual address space, code, security contexts, etc. A process can start multiple threads. Every process starts with a single thread called primary thread. You can create n number of threads in a process. Threads share the resources allocated to the process. A process is the parent and threads are his children.

3. What is multithreading in C#?

Performing multiple task at same time during the execution of a program, is known as multithreading.

4. What are interlocked functions?

Interlocked functions in .NET are useful in multithreading programs to safely change the value of shared variables. By default C# variables are not thread-safe. When we apply addition, subtraction or checking the value of variables multiple threads can corrupt the variables. For preventing these dirty reads, we can use Interlocked functions.

Interlocked functions can only work on int, long, double and float data types. Below are the some Interlocked functions.

1) Add(ref int location1, int value) : For safely adding the value into int variable.

2) Increment(ref int location1) : For safely increase the value of int variable by 1.

3) Decrement(ref int location1) : For safely decrease the value of int variable by 1.

4) Exchange(ref int location1, int value) : For safely set the new value into int value location1.

5) CompareExchange(ref int location1, int value, int comparand) : It first check the old value in location1 to comparand value. It both are equal then only it set the new value into int value location1.

5. Why need multi-threading in our project?

This is one of the most frequently asked Multi-Threading Interview Questions in C#.NET. Let us discuss this question

Multi-threading is used to run multiple threads simultaneously. Some main advantages are:

You can do multiple tasks simultaneously. For e.g. saving the details of the user to a file while at the same time retrieving something from a web service.

Threads are much lightweight than process. They don’t get their own resources. They used the resources allocated to a process.

Context-switch between threads takes less time than process.

6. What is the namespace used for multithreading in C#?

Using system.threading;

7. What are the advantage of multithreading in C#?

There are two main advantage of use of multithreading in C#. optimize the use of computer resources such as memory. Save time

8. What is AutoResetEvent and how it is different from ManualResetEvent?

AutoResetEvent is used when we have to unlock only one single thread from several waiting blocked threads.

Below are differences from ManualResetEvent.

ManualResetEvent is used for unblocks many threads simultaneously. But AutoResetEvent is used for unblocks only one single thread.

You have to call Reset() method manually after calling Set() method to reset the ManualResetEvent. But AutoResetEvent Set() method automatically calls the Reset() method.

9. What are the advantages and disadvantages of multithreading?

I think this Multi-Threading Interview Question is the most asked interview question in the dot net. So let us discuss the advantages and disadvantages

Advantages of multithreading:

To maintain a responsive user interface

It makes the efficient use of processor time while waiting for I/O operations to complete.

To split large, CPU-bound tasks to be processed simultaneously on a machine that has multiple CPUs/cores. 

Disadvantages of multithreading:

On a single-core/processor machine threading can affect performance negatively as there is overhead involved with context-switching.

Have to write more lines of code to accomplish the same task.

Multithreaded applications are difficult to write, understand, debug and maintain.

10. What are the classes used in system.threading namespace?

Thread thread pool monitor mutex

11. What is semaphore?

Semaphores are used when we have to restrict how many threads can enter a critical region. Semaphore is simply a int32 variable maintained by the kernel. We have initialize the Semaphore variable we specify the count how many threads can enter into critical region at a time. A thread waiting on a semaphore blocks when the semaphore is 0 and unblocks when the semaphore is greater than 0.

12. How can we create a Thread in C#?

To create a THREAD, we need to create an instance of Thread class (Thread class provided by System.Threading namespace) and to its constructor, we have to pass the function name as a parameter that we want the thread to execute. Then we need to call the start method of the Thread class. 

13. What is the use of thread class in C#?

Priority thread state isAlive current thread name etc.

14. What is Mutex and how it is different from another Synchronization mechanisms?

Mutex works similarly like AutoResetEvent and releases only one waiting thread at a time.

In the AutoResetEvent any thread can call the Set() method and unblock a waiting thread. But Mutex object remembers the thread which got the Mutex object and only that thread can release the Mutex.

Mutex object auto record the thread id which got the Mutex object and when a user calls the ReleaseMutex() method for releasing a Mutex object, it internally checks whether the releasing thread is same as the thread which got the Mutex object if yes, then only it releases the Mutex object else it throws an exception.

15. Why does a delegate need to be passed as a parameter to the Thread class constructor?

As we know the purpose of creating a Thread is to execute a function. We also know that a delegate is a type-safe function pointer meaning it points to a function that the thread has to execute. In short, all threads require an entry point to start execution. Any thread that we create will need an explicitly defined entry point i.e. a pointer to the function from where they should begin execution. So threads always require a delegate.

In the code below, we are not explicitly creating the ThreadStart delegate, then how is it working here?

Thread T1 = new Thread(Number.PrintNumbers);

It’s working in spite of not creating the ThreadStart delegate explicitly because the framework is doing it automatically for us. We can also rewrite the same line using delegate() keyword as shown below.

Thread T1 = new Thread(delegate() { Number.PrintNumbers(); });

We can also rewrite the same line using the lambda expression as shown below.

Thread T1 = new Thread(() => Number.PrintNumbers());

16. What are the methods used in thread class?

  • Join

  • Resume

  • Sleep

  • Spin wait

  • Suspended

  • Start

  • Interrupt

17. What is lock?

Lock is another synchronization mechanism in C# and one of the famous multi-threading interview questions in .NET. It restricts the critical region so that only one thread can enter a critical region at a time.

Locks needs an object to continue its operation. It apply a lock on a target object and only one thread can lock that target object at a time. Below is the example of lock object.

static object obj = new object();

public static void DoWork()




        Console.WriteLine("Multi threading Interview questions in C#");



18. How to pass the parameter in thread?

In the constructor of Thread, we can pass the method name which accepts only a single parameter. Then we have to pass the parameter into the Start method.

19. What is the thread pool class in C#?

The thread pool class is used, to perform task such as processing of asynchronous i/o and waiting on behalf of another thread.

20. What are concurrent collection classes?

ConcurrentBag :.NET Framework Class Library (FCL) provides four thread-safe collection classes in the System.Collections.Concurrent namespace.

ConcurrentQueue : It provides a thread-safe First-In-First-Out (FIFO) data structure. You can read more about ConcurrentQueue.

ConcurrentStack : It provides a thread-safe Last-In-First-Out (LIFO) data structure. You can read more about ConcurrentStack.

ConcurrentDictionary : It provides a thread-safe collection class to store key-value pairs. You can read more about ConcurrentDictionary.

ConcurrentBag : It provides a thread-safe collection class to store objects in unordered way. You can read more about ConcurrentBag.



1,160 points
8 6 3