Click below for Step by Step and Interview Q & A (dotnetinter) wrote,
Click below for Step by Step and Interview Q & A
dotnetinter

.NET interview questions: - Can you explain the term 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 Dotnet interview questions and answers


Regards,

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

Tags: .net and c# interview questions, .net and c# training, .net interview questions and answers, csharp interview questions, information technology questions, programming interview questions, volatile keywords
Subscribe

  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments