Basic concepts of Threading: Part II


The System.Threading Namespace

 

The System.Threading namespace provides classes and interfaces that enables multithreaded programming. This namespace includes a ThreadPool class that manages groups of threads, a Timer class that enables a delegate to be called after a specified amount of time, and a Mutex Class for synchronizing mutually-exclusive threads. System.Threading also provides classes for thread scheduling, wait notification, and deadlock resolution.

 

Some of the important classes in System.Threading namespace is discussed below.

 

Thread - Represents threads that execute within the runtime, to create and control threads.

 

ThreadPool - Posts work items to the thread pool. Queue work items for execution on the first available thread from the thread pool. One OS thread pool per process. Efficient use of thread resources.

 

Timeout - Contains timeout values for the system

 

Times - Specifies a delegate to be called at a specified time. The wait operation is performed by a thread in the thread pool.

 

Monitor - Provides the synchronization of threading objects using locks and wait/signals.

 

Mutex - A synchronization primitive that can also be used for interprocess synchronization. Wait can be used to request ownership of the mutex. The state of the mutex is signaled if no thread owns it.

 

The thread that owns a mutex can specify the same mutex in repeated wait function calls without blocking its executions. It must release the mutex as many times ti release ownership. If a thread terminates normally while owning a mutex, the state of the mutex is set to signaled and the next waiting thread gets ownership.

 

Life Cycle Methods

 

Starting a Thread

The thread class of  System.Threading Namespace represents a thread object. By using class object, we can create new threads, delete, pause and resume threads. The thread class creates a new thread and Thread.Start method starts a thread.

 

thread = new Thread(new TreadStart( writeData ));
thread.start();

 

where writeData is a function which will be executed by the program.

 

Killing a Thread

Thread class's Abort method is called to kill a thread permanently. Make sure IsAlive is called before Abort.

 

if( thread.IsAlive )

{

    thread.Abort();

} 

 

Pausing a Thread

Thread.Sleep method can be used to pause a thread for a fixed period of time.

 

thread.Sleep();

 

Suspending a Thread

The Suspend method of the thread class suspends a thread. The thread is suspended until Resume method is called.

 

if( thread.ThreadState == ThreadState.Running)
{
      thread.Suspend();
}
 

 

Resume a suspended Thread

The Resume method is called to resume a suspended thread. If the thread is not suspended, there will be no effect of Resume method.

 

if(thread.ThreadState == ThreadState.Suspended)
{
      thread.Resume();
}

 

Coming soon:- 

  • Part 3 -- Sample of MulthiThreaded Application using C#.Net.
  • Part 4 -- Important Properties and Methods of Threads.

Up Next
    Ebook Download
    View all
    Learn
    View all