1. Back To Blog

.NET interview questions: - What are the different types of threadstate in C# threading?

Sometimes people ask funny questions and they think that developers are walking c# encyclopedia. They expect .NET developers to know each and every method name, property name….sick, but then that's the reality.

 

One of our candidates who had bought from us Dotnet interview questions and answers  videos was recently stumped with one such question, what are the different types of states in c# threading.

 

In some of the .NET interviews I have seen people making it sophisticated by saying "Explain c# threading lifecycle?".

 

In case you are completely fresher in threading you can start from C# threading videos from here.

 

Such kind of questions needs stupid ways of remembering. There are 5 states of threading Running, Wait/Sleep/Join,Abort , Resume and Suspend. "Running" state is easy to remember, the others you can remember by the word "WARS". Below is an explanatory figure for the same.

 

threading

At the end I have also put a state flow chart diagram which explains how threads move from one state to other state.

 

Stop to Running: -When a new thread is created it start with a stop or in anunstarted state. If you start the thread by invoking the "Thread.Start" the thread is moves in to a running state.

threading

 

Running to Abort: - When a thread is in a running state it can move to 3 different states. If the thread terminates it will move to aborted state finally leading to stop state. You can also abort a thread by calling "Abort" function of the thread object this will move the thread to abort state.

 

threading

 

Running to Suspended: - As said previously your thread can move from running state to three different states one of them is suspended. If you call the "Suspend" method the thread is suspended and will be revoked back to the running state if you call "Resume" method.

 

threading4

 

threading5

 

Running to Wait/Sleep/Join: - A thread can go to "WaitSleepJoin"in 3 ways, the first one is when the "Monitors" "Wait" method is called.

 

threading6

 

Thread will again go back to "Running" state when the "Pulse" method of the "Monitor" object is called.

 

threading7

 

The second way by which "Thread" can go in "WaitSleepJoin" is by calling the "Sleep" method of the thread object.

 

threading8

 

After the specified millisecond elapses the thread comes back to "Running" state.

 

The third way is call the join method. When the join method is called the thread goes in the "WaitSleepJoin" state. Once the other thread finishes this dependent thread is released either to "Running" or "Aborted" depending on what kind of logic it's currently running.

 

threading9

 

Below is a complete life cycle of C# threads.

 

threading10

 

You can see the following video on C# threading of thread safety: -

 

 

In case you are completely fresh in C#, you can start from our C# training videos step by step.

Shiv Prasad Koirala

Visit us @ www.questpond.com or call us at 022-66752917... read more

We are on Social