This is really a hot topic now a days. This topic struck me when i was caught with a surprise call from a consultant and he wanted to me to rate myself from 1 to 10 in Threading. O, I was amazed..before that i used to ask poeple while conducting interviews to rate themselves in c#, vb.net, sql server, javascript. But to rate in a concept from 1 to 10 was astonishing to me and also on a topic which is mostly little bit known...so i have put in some good effort in it now and waiting for a call from the same consultant and try to be a confident guy now...
Threading/Multi-Threading:
Threading basically is a concept to make parallel execution a possibility. A thread is actually a unit of processor time consumption, means to say that a thread is something to which processor time is given. A process can contain different threads but the processor time is not given to processes but it is gifted only to threads inside them.
Multi-threading is probably one of the worst understood aspects of programming but in today's era, every programmer needs to know good bit of it to write an efficient application.
Lets write a simple threading program:
using System;
using System.Threading;
public class TestThreading
{
static void Main()
{
Thread worker = new Thread(Job);
worker.Start();
for (int i=0; i < 5; i++)
{
Console.WriteLine ("Main thread: {0}", i);
Thread.Sleep(1000);
}
}
static void Job()
{
for (int i=0; i < 10; i++)
{
Console.WriteLine ("worker thread: {0}", i);
Thread.Sleep(500);
}
}
}
OUTPUT:
Main thread: 0
Worker thread: 0
Worker thread: 1
Main thread: 1
Worker thread: 2
Worker thread: 3
Main thread: 2
Worker thread: 4
Worker thread: 5
Main thread: 3
Worker thread: 6
Worker thread: 7
Main thread: 4
Worker thread: 8
Worker thread: 9
We can see from the above output that there are two threads running actually, sometimes main thread and other times worker thread. The output in the above is random and the next time the progream may well give output in a different order.
Creation of the new Thread is done here:
Thread worker = new Thread(Job);
We can also write the above line in the following way:
Thread worker = new Thread(new ThreadStart(Job));
Pass parameters to Functions in Threads:
In C# 2.0, MS introduced the concept of parameterized threads which have the ability of passing parameters to the function which will be called by new thread.
Parmeterize thread can be created in the following way:
Thread worker = new Thread(new ParameterizedThreadStart(JobWithParamter));
worker.Start("Parametrized");
static void JobWithParameter(object parameter)
{
Console.WriteLine("worker thread: {0}", parameter.ToString());
}
Thus inorder to pass parameters to thread function, we are to pass parameter while calling 'Start'. This is to note that the parameter can only be an object type and nothing else.
Different Scenarios:
Scenario 1 --> We want to pass more than one parameters:
We can achieve this by creating a class with the desired number of fields which we want to pass as arguments to thread function and then passing that class's object into the 'Start'.
class Employee
{
string name;
int salary;
}
class abc
{
static void main()
{
Employee employee = new Employee(){name="gj";salary=50000};
Thread worker = new Thread(JobWithParamters);
worker.Start(employee);
}
static void JobWithParameters(object parameter)
{
Employee employee = (Employee)parameter;
Console.WriteLine("Employee Name: {0}", employee.name);
Console.WriteLine("Employee Salary: {0}", employee.salary.ToString());
}
}
Scenario 2: We need to pass the parameters without creating a new class:
I this scenario, we can create anonymous functions and thus pass any number of arguments.
class abc
{
static void main()
{
Thread worker = new Thread(new delegate() {JobWithActualParameters("Gj", 50000);});
worker.Start(employee);
}
static void JobWithActualParameters(string name, int salary)
{
Console.WriteLine("Employee Name: {0}", name);
Console.WriteLine("Employee Salary: {0}", salary.ToString());
}
}
We will cover handling Exceptions in Threading in our next article