• Now Online : 98
  • admin@codemyne.net


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()
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
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
    public static void m2()
        for (int k = 11; k <= 20; k++)
            Thread.sleep(500);  //milli seconds
//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);

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.
Example on thread synchronization with banking concept: steps:
  • 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()
        // check the balance
        SqlConnection cn=new SqlConnection("data source=localhost;initial catalog=renuka; integrated security=SSPI");
        SqlCommand c1=new SqlCommand("select Bal from GTB where Accno=101",cn);
        object obj=c1.ExecuteScalar();
        int bal=int.Parse(obj.ToString());
        int wd=20000;
            SqlCommand cmd=new SqlCommand("update GTB set Bal=Bal-"+wd+" where Accno=101",cn);
            Console.WriteLine("Take the money");
        }//closing if
            Console.WriteLine("Insufficient balance");
    }//ATM logic
static void Main(string[] args)
    ThreadStart ts=new ThreadStart(Test.ATMlogic);
    Thread h=new Thread(ts);
    Thread w=new Thread(ts);

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.