.NET interview questions: – Describe Volatile Keyword?

Volatile Keyword: – The Volatile keyword ensures that whichever the variable data you are accessing currently is up to date or is sinking with main memory which is being updated.

In other words you will use Volatile Keyword in scenarios when you are doing  multi-threading applications and especially when you are accessing data which is being updated concurrently by multiple threads.

Now, let’s see a scenario where the actual use of the Volatile Keyword exists.

In order to see it practically and understand the concept and the use of the  Volatile Keyword just follow the following steps.
Step1: – create a simple Console Application for that just go to >>
File
>> New >> Project >> Windows >> Select Console
Application.

Step2: – Import the below Namespace.

using System.Threading;

Step3: – Now, just simply add the below code snippet in to your
Program.cs file of Console Application.

class Program
{
private bool _loop = true;
static void Main(string[] args)
{
//Calling the SomeThread method in
//MultiThreaded Manner.
Program test1 = new Program();
Thread obj = new Thread(SomeThread);
obj.Start(test1);
//Pauses for 20 ms
Thread.Sleep(20);
//Setting the loop value to false.
test1._loop = false;
Console.WriteLine("Step2 :- The Value is Set To False");
}
//Simple Method called SomeThread.
private static void SomeThread(object o1)
{
Program o = (Program)o1;
Console.WriteLine("Step1 :- Entered The Loop");
//This will run untill the _loop value is true.
while (o._loop)
{ 
}
Console.WriteLine("Step3 :- Exited The Loop");
Console.ReadLine();
}

Let me just explain flow of the program, when you execute your program first the below code line will get executed.

Program test1 = new Program();
Thread obj = new Thread(SomeThread);
obj.Start(test1);
Thread.Sleep(20);

Once the above line of thread is executed it should display us the below code of
line which is inside the SomeThread Method.

Program o = (Program)o1;
Console.WriteLine("Step1 :- Entered The Loop");

Now the below while loop will run continuously until the _loop value is set to false.

while (o._loop)
{ 
}

Now, the below line of code will get executed and the value of the _loop will be set to false.

test1._loop = false;
Console.WriteLine("Step2 :- The Value is Set To False");

Once the value of the _loop is set to false, now the while loop will get
terminated and the below line of code will get executed.

Console.WriteLine("Step3 :- Exited The Loop");

Step4: – now, just run the Console Application and you will see the
result like below diagram.

Note: – Please run your application in Release mode.

In the above diagram of output you can clearly see that the step3 has not been executed.

Step5: – Now, just make necessary changes to the program.cs file
like below code snippet.

class Program
{
private volatile bool _loop = true;
static void Main(string[] args)
{
//Calling the SomeThread method in
//MultiThreaded Manner.
Program test1 = new Program();
Thread obj = new Thread(SomeThread);
obj.Start(test1);
//Pauses for 20 ms
Thread.Sleep(20);
//Setting the loop value to false.
test1._loop = false;
Console.WriteLine("Step2 :- The Value is Set To False");
}
//Simple Method called SomeThread.
private static void SomeThread(object o1)
{
Program o = (Program)o1;
Console.WriteLine("Step1 :- Entered The Loop");
//This will run untill the _loop value is true.
while (o._loop)
{ 
}
Console.WriteLine("Step3 :- Exited The Loop");
Console.ReadLine();
}

Now, just your application and will find result like below diagram.

Also see the following video on volatile keyword in C# and .NET: –


View our other most important interview questions and answers for .NET

Regards,

Visit author’s blog for more Most asked Dotnet interview questions

 

Advertisements

About c# and .NET Interview questions

This blog is for developers who want to crack .NET and C# interviews. It has all tips and tricks needed to crack .NET interviews , C# interview , SQL Server interview , Java interview , WCF Interview , Silverlight interview , WPF interview , LINQ interview , Entity framework Interview. Do not forget to watch our Learn step by step video series. Learn MVC in 16 hours:- https://www.youtube.com/watch?v=Lp7nSImO5vk Learn AngularJS Step by Step:- https://www.youtube.com/watch?v=0kmdjqgO9IY Learn Design Pattern in 8 hours:- https://www.youtube.com/watch?v=YDobmucohqk Learn C# and .NET in 60 days:- https://www.youtube.com/watch?v=yh2SrzCkNQA Learn MSBI in 32 hours:- https://www.youtube.com/watch?v=mGPJx3ocFgg Learn SharePoint Step by Step in 8 hours:- https://youtu.be/C2fW76SwJNU
This entry was posted in Uncategorized and tagged , , , , , , , , . Bookmark the permalink.

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