How to use multi threading in VB.NET

In this article you will learn What is multi threading in VB.NET.
  • 7206

Multithreading is mostly one of the cheapest understood view of programming.Multithreading is most important technique that is used in .net technology.Multithreading is a part of the operatin system as we know in multithreading many processors run at a time. Multi threading play an important role in .net In multithreading many CPU running simultaneously.Using multi-threading the CPU utilisation increases and increases the CPU speed.

Before multithreading, Generally there was always one thread process running for each process in the O.S.If you think of processes running in parallel in an operating system the we use the same kind of thinking as we used in single process,this is the simplest way to visulise the threading.

Example of multithread

using System;
using System.Threading;

public class Test
{
    static void Main()
    {
        ThreadStart job = new ThreadStart(ThreadJob);
        Thread thread = new Thread(job);
        thread.Start();

        for (int i=0; i < 5; i++)
        {
            Console.WriteLine ("Main thread: {0}", i);
            Thread.Sleep(1000);
        }
    }
        static void ThreadJob()
    {
        for (int i=0; i < 10; i++)
        {
            Console.WriteLine ("Other thread: {0}", i);
            Thread.Sleep(500);
        }
    }
}

Output of the Given thread:

Main thread: 0
Other thread: 0
Other thread: 1
Main thread: 1
Other thread: 2
Other thread: 3
Main thread: 2
Other thread: 4
Other thread: 5
Main thread: 3
Other thread: 6
Other thread: 7
Main thread: 4
Other thread: 8
Other thread: 9

Now there are many problem arises with multi-threading like

1.Deadlockl state

2.Waiting and pulsing

How to avoid Deadlock state :

As we know about Deadlock.deadlock is a state when each process is depend to other process and no one process is executed completely then deadlock state is occur.In this state whole process wait for other processes execution.
This coding is helpful for avoiding multithreading in .net.
 

using System;
using System.Threading;
 
public class Test
{
  static readonly object firstLock = new object();
  static readonly object secondLock = new object();
    
    static void Main()
    {
        new Thread(new ThreadStart(ThreadJob)).Start();
        
        Thread.Sleep(500);
        Console.WriteLine ("Locking secondLock");
        lock (secondLock)
        {
            Console.WriteLine ("Locked secondLock");
            Console.WriteLine ("Locking firstLock");
            lock (firstLock)
            {
                Console.WriteLine ("Locked firstLock");
            }
            Console.WriteLine ("Released firstLock");
        }
        Console.WriteLine("Released secondLock");
    }
    
    static void ThreadJob()
    {
        Console.WriteLine ("\t\t\t\tLocking firstLock");
        lock (firstLock)
        {
            Console.WriteLine("\t\t\t\tLocked firstLock");
            
            Thread.Sleep(1000);
            Console.WriteLine("\t\t\t\tLocking secondLock");
            lock (secondLock)
            {
                Console.WriteLine("\t\t\t\tLocked secondLock");
            }
            Console.WriteLine ("\t\t\t\tReleased secondLock");
        }
        Console.WriteLine("\t\t\t\tReleased firstLock");
    }
}Output of the given threading program:Locking firstLock
Locked firstLock
Locking secondLock
Locked secondLock
Locking firstLock
Locking secondLockOne by One execution occur.This strategy is handle very carefully.

Categories

More Articles

© 2020 DotNetHeaven. All rights reserved.