.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

Static variables can induce threading problems?

Posted By: Jean Paul     Posted Date: February 19, 2011    Points:2   Category :C#
Can we use static variables in a multithreaded environment.

Do anybody have a problem example?

Responses
Author: prakash1598             
Posted Date: February 22, 2011     Points: 5   

yes of course one can use the static variable or method in the multithreading application it is depend on the
your require ment

Author: prakash1598             
Posted Date: February 22, 2011     Points: 5   



if you want to go through the code then here i have done some thing for you


class thredapp
{
public void show()
{
int a=10 ;// normal variable
Console.WriteLine("the value of the a is "+a );
}
public void display()
{
int a=3;// normal varialbe
Console.WriteLine("the value of the a is"+a );
}
static void Main(string[] args)
{
thredapp ob = new thredapp ();//
ThreadStart th1 = new ThreadStart(ob.show );
ThreadStart th2 = new ThreadStart(ob.display);
Thread td1 = new Thread(th1 );
Thread td2 = new Thread(th2 );
td1.Start();
td2.Start();
Console.ReadLine();

}
}


**********************************************************************
same application using the static variable and minor changes

class thredapp
{
public static int a = 2;// here i have declared it as static variable
public void show()
{
//so here is nothing to do because i want to see the same value in the both method
Console.WriteLine("the value of the a is "+a );
}
public void display()
{
//so here is nothing to do because i want to see the same value in the both method

Console.WriteLine("the value of the a is"+a );
}
static void Main(string[] args)
{
thredapp ob = new thredapp ();
ThreadStart th1 = new ThreadStart(ob.show );
ThreadStart th2 = new ThreadStart(ob.display);
Thread td1 = new Thread(th1 );
Thread td2 = new Thread(th2 );
td1.Start();
td2.Start();
Console.ReadLine();

}
}
}
i hope it will tackle your problem




Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend