Wednesday 18 November 2015

Threading Overview in C#



What threads really are ?


A thread is a sequence of instructions executed within the context of a process. One can also say threads are the smallest possible part of the process that can be handled or controlled independently by a scheduler.

Multiple threads within a process share the memory, execution instruction, context data and other system resources.



Multi-threaded Processes :


Creating a process includes starting the process running at an instruction point. This is normally known as the primary or main thread.


If we want our process to do more than one thing, like query a Web Service and write to a database at the same time then multi-threading can help us.


We can split a process to share the time slice allocated to it with different thread.
The concept of spawning new threads within the same process is called free threading.


Code Time


Starting and making  a thread 


Step 1 : Creating a thread

using only with thread class :

Thread workerThread = new Thread ( function_name_for_which_thread_is made );


 using ThreadStart Delegate

The ThreadStart delegate works similar to  above. It enables starting a thread without passing any argument to the target method.


Thread t2 = new Thread(new ThreadStart(function_name_for_which_thread_is made ));


 Step 2 Start the thread : 


workerThread.Start( );
You can also have a parameter thread start, then pass the required value to the function at the time of thread start.

workerThread.Start(50);

Now, if thread need its CPU time to wait for a resource, or for some other reason, so the programmer may choose to make the thread sleep, which result in the thread being packed in the TLS and provide time for other process meanwhile making for code more efficient. This is simply done by :


Thread.Sleep(duration_in_milliseconds_for which you want your thread to sleep);



 Example :


Thread.Sleep(5000);

 // It will make the thread sleep for 5 seconds.


Putting altogether :

using System; 
using System.Threading;
namespace threading
{
class student
{
public void thread1()
{
Console.WriteLine("First thread of student class and method thread1 is running");
Thread.Sleep(1000);
Console.WriteLine("First thread of student class and method thread1 has ended");
}

public void thread2()
{
Console.WriteLine("Second thread of student class and method thread2 is running");
Thread.Sleep(2000);
Console.WriteLine("Second thread of student class and method thread2 has ended");
}

public static void Main(string[] args)
{
student st = new student();
Thread = new Thread(new ThreadStart(st.thread1)); //creating thread T1
T1.Start(); // Starting thread T1
Console.WriteLine("First thread T1 started");
Thread T2 = new Thread(new ThreadStart(st.thread2));//creating thread T2
T2.Start();// Starting thread T2
Console.WriteLine("second thread T2 started");
Console.ReadLine();
}
}
}






Some Important Methods of Thread Class 


  • Suspend:-It is used ,for suspending a thread.
  • Join:- Block a thread until another thread has terminated.
  • Resume:-It is used for resuming a thread which has been suspended earlier.
  • Interrupt:-It is used to interrupt the thread,which is in the wait sleep join state.
  • SpinWait:- It is used to make a thread wait the number of times specified in Iteration parameter.
  • Abort:-It is used to terminate the thread as soon as possible.
  • Interrupt:-It is used to interrupts the current thread from a suitable wait period.
  • start:- start the thread.


No comments:

Post a Comment