Introduction
Multi means many, thread means a process. Process indicates either part of the program or a complete program. Standard defination
of multithreading is a concept of executing more than one process simultaneously.
Examples for multithreaded softwares: ATM server, IIS server, Oracle server, SQL server, Biztalk server, Share Point server...etc
By default timer control is multithreaded. Sharing CPU cycles efficiently is also called as multithreading. One cycle is also called as one instruction.

Basically operating systems are divided into two types:
- Single Threaded operating systems (DOS, UNIX)
- Multi Threaded operating systems (windows, linux, andriod, symbian)
To work with multithreading DotNet introduced system.threading.namespace which contains 3 important classes.
1.Thread: It is a predefined class, which is responsible for maintaining thread life cycle.
2.Threadstart: Which is a predefined delegate and responsible for creating the threads.
3.Mutex: Mutex is required in thread synchronization.
4-steps for creating a thread:
1. Write a class with a set of methods
class test { public static void print() { logic; } }2. Create an object for ThreadStartDelegate, with the address of a method.
ThreadStart ts = new ThreadStart(Test.Print);
3. Create an object for ThreadClass with the help of ThreadStart object.
Thread t = new Thread(ts);
4. Start Thread
t.start();A multithreaded application for executing 2 methods simultaneously: Here in the below program the two threads are accessing two different processes(methods).
- Open a console application.
- Use namespace as System.Threading.
- Write the code in general declaration(above main method).
- Write the code for main method then press control f5.
- Observe that two processes are executing simultaneously.
//code in general declaration class Test { public static void m1() { for (int i = 1; i <= 10; i++) { Console.Write(i + " "); Thread.sleep(500); //milli seconds }//for }//m1 public static void m2() { for (int k = 11; k <= 20; k++) { Console.WriteLine(k); Thread.sleep(500); //milli seconds }//for }//m2 } //code for main method static void main() { ThreadStart ts1 = new ThreadStart(Test.m1); ThreadStart ts2 = new ThreadStart(Test.m2); Thread t1 = new Thread(ts1); Thread t2 = new Thread(ts2); t1.start(); t2.start(); Console.ReadKey(); }
Working with Thread-Synchronization:
- Whenever more than one thread is trying to access the same process(method), then there is a possibility for runtime problems, to overcome this problem we require thread synchronization.
- Thread synchronization is the concept of allowing only one thread into one process at a time.
- In case if process is busy then threads need to wait in the queue.
- Waiting threads will be executed based on the priority(importance).
- To work with thread synchronization, we required a predefined class called as mutex(Mutually Exclusive).
- Mutex class is having two methods waitone() and releasemutex().
- The code which is written inside of this two methods is called as synchronized code.
- Synchronized code is accessible only to one thraed at a time.
- Open Sqlserver, Create a database, and create table as follows
- create table GTB(accno int, bal int)
- Insert into GTB values(101, 10000)
- Open console apllication project
- Enter the namespace as using system.data.sqlclient
- Code in general declaration(write before main())
static Mutex m=new Mutex(); class Test { public static void ATMlogic() { m.WaitOne(); // check the balance SqlConnection cn=new SqlConnection("data source=localhost;initial catalog=renuka; integrated security=SSPI"); cn.Open(); SqlCommand c1=new SqlCommand("select Bal from GTB where Accno=101",cn); object obj=c1.ExecuteScalar(); int bal=int.Parse(obj.ToString()); Thread.Sleep(1000); int wd=20000; Thread.Sleep(1000); if(bal-wd>=0) { Thread.Sleep(1000); SqlCommand cmd=new SqlCommand("update GTB set Bal=Bal-"+wd+" where Accno=101",cn); Thread.Sleep(1000); cmd.ExecuteNonQuery(); Thread.Sleep(1000); Console.WriteLine("Take the money"); Thread.Sleep(1000); }//closing if else { Console.WriteLine("Insufficient balance"); Thread.Sleep(1000); } m.ReleaseMutex(); }//ATM logic }//Test static void Main(string[] args) { ThreadStart ts=new ThreadStart(Test.ATMlogic); Thread h=new Thread(ts); Thread w=new Thread(ts); h.Start(); w.Start(); Console.ReadKey(); }
The above program shows the ATM transactions of wife and husband with a joint account in GTB bank. The transactions are performed using thread-synchrnization, Which allows only one thred into one process at a time. The database updated when any one of them drawn the money.
Comments/Suggestions are invited. Happy coding......!
Comments Post a Comment
VINAY KUMAR GUPTA 6/6/2015 (IST) / Reply
Very nice i don't have words to praise you really mind blowing this will help to create high level project.