C# Multithreading interview questions with answers Part 1

What is the difference between Process and Thread?

  • A process is what the operating system uses to facilitate the execution of a program by providing the resources required.
  • A process can start multiple threads.
  • Every process is start with a single thread called primary thread.
  • A single process can have multiple threads.
  • Threads share the resources allocated to process.
  • A process is the parent and threads are his children.

How to create and start a Thread in C#?

We have to use the Thread class provided by System.Threading namespace. In the constructor of the class, we have to pass the method name which we want to run in separate thread. After than we have to call the start method of Thread class. Below is the example.

using System;
using System.Threading;

namespace d2cThreadSample
{
class Program
{
static void Main(string[] args)
{
Thread objThread1 = new Thread(d2cThreadMethod);
objThread1.Start();
}

private static void d2cThreadMethod()
{
Console.WriteLine("Welcome to desire2code.com");
Console.ReadLine();
}

}
}

How to pass 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 parameter into the Start method. Below is the example.
using System;
using System.Threading;

namespace d2cThreadSample
{
class Program
{
static void Main(string[] args)
{
Thread objThread1 = new Thread(d2cThreadMethod);
objThread1.Start("Welcome to desire2code.com");
}

private static void d2cThreadMethod(object inputString)
{
Console.WriteLine(inputString);
Console.ReadLine();
}

}
}

What is Deadlock? What are four necessary conditions for Deadlock?

A deadlock is a situation in which two or more processes cannot complete their work.They are blocked and each thread waiting for other threads to release resource they want. For example, we have two threads T1 and T2 and we have two resources R1 and R2. Both threads want to work with both resources. But in opposite fashion, T1 want to work with R1 first and then with R2. T2 want to work with R2 first and then with R1. Both threads starts at the same time. T1 locked R1 while at the same time T2 locked R2. Now T1 want a lock on R2 which is already locked by T2 and T2 want a lock on R1 which is already locked by T1. Both threads waiting for other threads to unlock the resources. Both threads are waiting for infinite time and cann’t continue forever.

deadlock

Deadlock Conditions
mutual exclusion
The resources involved must be unshareable; otherwise, the processes would not be prevented from using the resource when necessary.
hold and wait or partial allocation
The processes must hold the resources they have already been allocated while waiting for other (requested) resources. If the process had to release its resources when a new resource or resources were requested, deadlock could not occur because the process would not prevent others from using resources that it controlled.
no pre-emption
The processes must not have resources taken away while that resource is being used. Otherwise, deadlock could not occur since the operating system could simply take enough resources from running processes to enable any process to finish.
resource waiting or circular wait
A circular chain of processes, with each process holding resources which are currently being requested by the next process in the chain, cannot exist. If it does, the cycle theorem (which states that “a cycle in the resource graph is necessary for deadlock to occur”) indicated that deadlock could occur.

What is immutable object? How to create an immutable object?

An immutable object is an object which states cannot be changed after creation. Immutable objects are useful in concurrent programming as they are thread-safe. Immutable objects are objects which once loaded cannot be changed / modified by any way external or internal.

Immutable objects can be created only from immutable classes. To create an immutable class is a three step process:

Step 1: Remove the setters of the class, only have getters.

class ImmutableObject
{
private string _applicationSessionId;
public string applicationSessionId
{
get { return _applicationSessionId; }
}
}

Step 2: Provide parameters via constructor.

class ImmutableObject
{
private string _applicationSessionId;
public string applicationSessionId
{
get { return _applicationSessionId; }
}
public ImmutableObject(string iapplicationSessionId)
{
_applicationSessionId = iapplicationSessionId;
}
}

Step 3: Make the variables of the property READONLY

class ImmutableObject
{
private readonly string _applicationSessionId;
public string applicationSessionId
{
get { return _applicationSessionId; }
}
ImmutableObject(string iapplicationSessionId)
{
_applicationSessionId = iapplicationSessionId;
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s