Hello Friends,
I am back with a new post related to threads again.But this time i am going to explain them in a very simple manner for the perspective of a new bee in coding :)
Lets start without talking too much.Well we all know that whatever we write in our program runs in a MAIN THREAD which actually is nothing but acts like the father of all its children threads.
Main function :
static void Main(string[] args)
{
//Everything written here will run in the main thread.
}
Creation of thread :
We use namespace System.Threading as it contains all the necessary classes & ,methods which are used for threads.
static void Main(string[] args)
{
//creating thread
ThreadStart thStrt = new ThreadStart(WriteOnScreen);
Thread thread1 = new Thread(thStrt);
thread1.Start();
I am back with a new post related to threads again.But this time i am going to explain them in a very simple manner for the perspective of a new bee in coding :)
Lets start without talking too much.Well we all know that whatever we write in our program runs in a MAIN THREAD which actually is nothing but acts like the father of all its children threads.
Main function :
static void Main(string[] args)
{
//Everything written here will run in the main thread.
}
Creation of thread :
We use namespace System.Threading as it contains all the necessary classes & ,methods which are used for threads.
static void Main(string[] args)
{
//creating thread
ThreadStart thStrt = new ThreadStart(WriteOnScreen);
Thread thread1 = new Thread(thStrt);
thread1.Start();
}
public static void WriteOnScreen()
{
Console.WriteLine("Hello i am from thread.");
}
You must have to notice 2 points in above code snippet -
1. We need Thread Start object to pass in thread.
2. Thread Start object contains Method name. Method must be STATIC which we are passing in it.
Calling Method with parameters in thread :
We have 2 popular ways to do this-
- Using delegate object. -
public static void WriteThisOnScreen(object toWrite)
{
Console.WriteLine("Hello i am from parameterized thread. "+toWrite);
}
static void Main(string[] args)
{
//creating thread with parameters
ThreadStart threadStrt = delegate{ WriteThisOnScreen("Pragya"); };
Thread thread2 = new Thread(threadStrt);
thread2.Start();
}
- Using Parameterized Thread object.
static void Main(string[] args)
{
//parameterized thread
ParameterizedThreadStart paramThS = delegate(object o) { WriteThisOnScreen("Pragya"); };
paramThS.Invoke("Pragya");
}
Understanding Synchronization :Use of lock -
Suppose there is a function like this:
public static void PerformOperation(int a, int b)
{
int resultOfDivision = a / b;
Console.WriteLine("Result of division is " + resultOfDivision);
a= 0;
b= 0;
}
static void Main(string[] args)
{
ThreadStart threadStrt3 = delegate { PerformOperation(10,20); };
Thread thread3 = new Thread(threadStrt3);
ThreadStart threadStrt4 = delegate { PerformOperation(10,20); };
Thread thread4 = new Thread(threadStrt4);
thread3.Start();
thread4.Start();
}
Now if i will call this method using two different threads then it will lead an exception :
DivideByZero Exception.
Do u know why??? Because when thread 3 was making b=0 at that time thread 4 was doing a/b which gives divide by 0 exception.
So in such cases where you need to control that at a time only one thread would access a variable or certain piece of code we use mutual exclusion mutex lock using keyword lock.
Now problem would be resolved by using this :
public static void PerformOperation(int a, int b)
{
lock(this)
{
// At a time only one thread will enter in this code region.....
int resultOfDivision = a / b;
Console.WriteLine("Result of division is " + resultOfDivision);
a= 0;
b= 0;
}
}
Very simple solution :)
Lets talk about a problem which is caused due to wrong use of locks.
We all must have heard about it.It is DEADLOCK..
ummm no point is How a developer can ruin his/her code by using locks in wrong way?
answer is : See this example
public static void MyMethod()
{
lock(a)
{
// some piece of code...
lock(b)
{
// some piece of code...
}
}
}
public static void SomeOtherMethod()
{
lock(b)
{
// some piece of code...
lock(a)
{
// some piece of code...
}
}
}
Suppose thread 1 is running MyMethod() and locking a & processing while at the same time thread 2 is running in SomeOtherMethod() and locking b & processing. So are you getting the problem???? yep you are right. when thread 1 will go to line where lock(b) is required it will wait until it get the lock for b & at the same time when thread 2 will come to the line where lock(a) is required it will also wait until get lock for a. where lock of b is acquired by thread 2 & lock of a is acquired by thread 1 both threads will make DEADLOCK.
so the point of advice is :)
do not use too muck locks without thinking.
Well Thanks for reading this.hope it was needful.
Soon i will publish new and interesting small articles.
Thanks.
Pragya Sharma
Software Engineer
INDIA
thanku Pragya sharma..your posts are really helpful..
ReplyDeletekeep this good working going on
good one
ReplyDelete