Friday, September 6, 2013

What are differences between Thread and Process and what is the syntax to use threads in ASP.NET?

A process is an executing instance of an application while a thread is a path of execution within a process. For example, when you open Microsoft Word, you start a process that runs Word. A process can consist of multiple threads. When you start Word, the operating system creates a process and begins executing the primary thread of that process. A thread can do anything a process can do.

Here’s a summary of the differences between threads and processes:

1. Threads are easier to create than processes because they don't require a separate address space and share the same address space within a process whereas processes don’t share the same address space or heap memory.

2. Multithreading requires careful programming since threads share data structures (which allow them to read from and write to the same data structures) that should only be modified by one thread at a time.  Unlike threads, processes don't share the same address space. Communication between processes also known as inter-process communication (IPC) is also quite difficult and resource-intensive.
   
3. Threads are considered ‘lightweight’ because they use far less resources than processes and are used for small tasks while processes are used for more ‘heavyweight’ tasks basically for the execution of applications.
   
4. Processes are independent of each other but as threads share the same address space are interdependent. So, caution must be taken to prevent threads step over each other.
   
A common use for multithreading or multiprocessing is to maintain a responsive user interface for users while a time-consuming task executes. If the time-consuming task runs on a parallel "worker" process or thread, the main thread is free to continue processing keyboard and mouse events.

You can create and run following sample program to see the application of threads:

using System;
using System.Threading;
class TestThread
{
  static void Main(  )
  {
    Thread t = new Thread (WriteThreadB);          // Kick off a new thread
    t.Start();    // running WriteThreadB (  )
    // Simultaneously, do something on the main thread.
    for (int i = 0; i < 1000; i++)
       Console.Write ("Thread A ");
  }
  static void WriteThreadB (  )
  {
      for (int i = 0; i < 1000; i++)
          Console.Write ("Thread B");
  }
}

2 comments:

  1. Hello, i think that i saw you visited my blog thus i got here to return the want?.I am trying to to find things to improve my web site!I
    guess its adequate to make use of a few of your ideas!!



    Feel free to visit my website :: Star Wars Rebel Recon Hack

    ReplyDelete
  2. Howdy I am so glad I found your site, I really found you by accident, while I was
    looking on Digg for something else, Anyways I am here
    now and would just like to say thanks a lot for a fantastic post and a all round thrilling blog (I
    also love the theme/design), I don't have time to read it all at the minute but I have book-marked it and also included your RSS feeds, so when I have
    time I will be back to read more, Please do keep up
    the superb job.

    my blog post - free minecraft games

    ReplyDelete