Tuesday, March 17, 2009

Threading C#

The .NET Framework defines two types of threads:
foreground and background.

By default when you create a thread, it is a foreground thread, but you can change it to a background thread.
The only difference between a foreground and background thread is that a background thread will be automatically terminated when all foreground threads in its process have stopped.

Thread Methods:
Suspend() -> Suspends the execution of a thread till Resume() is called on that.
Resume() -> Resumes a suspended thread. Can throw exceptions for bad state of the thread.
Sleep() -> A thread can suspend itself by calling Sleep(). Takes parameter in form of milliseconds. We can use a special timeout 0 to terminate the current time slice and give other thread a chance to use CPU time
Join()-> Called on a thread makes other threads wait for it till it finishes its task. States of a Thread.

States of a Thread:
States of a thread can be checked using ThreadState enumerated property of the Thread object which contains a different value for different states.
Aborted -> Aborted already.
AbortRequested -> Responding to an Abort() request.
Background -> Running in background. Same as IsBackground property.
Running -> Running after another thread has called the start()
Stopped -> After finishing run() or Abort() stopped it.
Suspended -> Suspended after Suspend() is called. Unstarted -> Created but start() has not been called.
WaitSleepJoin -> Sleep()/Wait() on itself and join() on another thread. If a thread Thread1 calls sleep() on itself and calls join() on the thread Thread2 then it enters WaitSleepJoin state. The thread exists in this state till the timeout expires or another thread invokes Interrupt() on it.

Properties of a Thread:
Thread.CurrentThread -> Static method gives the reference of the thread object which is executing the current code.
Name -> Read/Write Property used to get and set the name of a thread
ThreadState -> Property used to check the state of a thread.
Priority -> Property used to check for the priority level of a thread.
IsAlive -> Returns a Boolean value stating whether the thread is alive or not.
IsBackground -> Returns a Boolean value stating the running in background or foreground.


ThreadPriority levels: of thread is set or checked by using an enumeration i.e. ThreadPriority. The valid values are for this enumeration are;
Highest AboveNormal Normal BelowNormal Lowest

Synchronization in Threads:-
Using the Lock Keyword
Converting the Code to Enable Synchronization using the Lock Keyword

public void PrintNumbers()
{
lock (this)
{
for (int i = 0; i < 5; i++)
{
Thread.Sleep(100);
Console.Write(i + ",");
}
Console.WriteLine();
}
}

-Using the Monitor Type
The C# lock keyword is just a notation for using System.Threading.Monitor class type. The lock scope actually resolves to the Monitor class after being processed by the C# compiler.
Converting the Code to Enable Synchronization using the Monitor Class

public void PrintNumbers()
{
Monitor.Enter(this);
try
{
for (int i = 0; i < 5; i++)
{
Thread.Sleep(100);
Console.Write(i + ",");
}
Console.WriteLine();
}
finally
{
Monitor.Exit(this);
}
}

Ref : http://www.codeproject.com/KB/threads/thread_synchronization.aspx

No comments: