It is a concept of performing multiple tasks over a certain period of time by executing them concurrently.
It forms subset of Multi-tasking. It is the ability of a program to manage its use by more than one user at a time and to even manage multiple requests by the same user without having to have multiple copies of the programming running in the computer.
It is a thread of execution in a program. One or more threads run in an AppDomain. An AppDomain basically provides an isolated region in which code runs inside of a process. Each AppDomain is started with a single thread, but can create additional threads from any of its threads. ‘System.Threading’ has all the classes related to implement threading. Any .NET application which wants to implement threading has to import this namespace.
There are three versions of Thread join-
· Thread.join(int) this returns a bool value.
· Thread.join(TimeSpan) this returns a bool value.
The Thread.Join method is useful to determining if a thread has completed before starting another task. The Join method waits a specified amount of time for a thread to end. If the thread ends before the time-out, Join returns true; otherwise it returns false. Once we call join, the calling procedure stops and waits for the thread to signal that it is done.
Thread.Join(int) and Thread.Join(TimeSpan) ensures that threads do not wait for a long time. If it exceeds a specific time, which is provided in integer or timespan the waiting thread will start.
It provides methods which help protect against errors that can occur when the scheduler switches contexts when two threads are executing concurrently on separate processors.
Monitor objects expose the ability to synchronize access to a region of code by taking and releasing a lock on a particular object using the Monitor. Lock statement is provided in order to simplify access to monitor object.
The WaitHandle class encapsulates Win32 synchronization handles, and is used to represent all synchronization objects in the runtime that allow multiple wait operations. There are three kind of wait modes:
When a thread wants to release a Wait handle it can call set method. We can use mutex objects to avail for the following modes. Mutex objects are synchronization objects whose state is set to signaled when it is not owned by any thread, and nonsignaled when it is owned. Threads request ownership of the mutex object when they require exclusive access to a resource because only one thread can own a mutex object at any time, other threads must wait for ownership of a mutex object. If a thread terminates normally while owning a mutex object, the state of the mutex object is said to be signaled and the next waiting thread gets ownership.
ManualResetEvent and AutoResetEvent
Threads that call one of the wait methods of a synchronization event must wait until another thread signals the event by calling the Set method. There are two synchronization event classes. Threads set the status of ManualResetEvent instances to signaled using the Set method. Threads set the status of ManualResetEvent instances to no signaled using the Reset method or when control returns to a waiting WaitOne call. Instances of the AutoResetEvent class can also be set to signaled using Set, but they automatically return to no signaled as soon as a waiting thread is notified that the event became signaled.
Reader Writer Locks
We may want to lock a resource only when data is being written and permit clients to simultaneously read data when data is not being updated. The ReaderWriterLock class enforces exclusive access to a resource while a thread is modifying the resource, but it allows nonexclusive access when reading the resource. Reader Writer Locks are a good alternative to exclusive locks that cause other threads to wait, even when those threads do not need to update data.
A good and careful planning can avoid deadlocks. There are so many ways Microsoft has provided by which we can reduce deadlocks.
Thread and Process
A process, in the simplest terms, is an executing program. One or more threads run in the context of the process.
protected void Button1_Click(object sender, EventArgs e)
Thread pthread1 = new Thread(Thread1);
Thread pthread2 = new Thread(Thread2);
public void Thread1()
int pint = 0;
pstr = "This is first thread";
pint = pint++;
while (pint < 5);
public void Thread2()
int pint = 0;
pstr = "This is second thread";
pint = pint++;
while (pint < 5);
Thread Priority can be changed by using
Threadname.Priority = ThreadPriority.Highest;
Following are different types of Priority provided by .NET:
“Thread.CurrentThread” refers to the current thread running in the method. “CurrentThread” property is used to display information about the thread on which it is running. Thread’s execution can be paused by calling the Thread.Sleep method. This method takes an integer value that determines how long the thread should sleep. We can also place a thread into the sleep state for an indeterminate amount of time by calling Thread.Sleep(Timeout.Infinite);. To interrupt this sleep we can call the Thread.Interrupt method. Thread.Abort() stops the thread execution at that moment itself.
This window is only seen when the program is running in debug mode. In windows one of the windows is “Threads”.
Daemon thread runs in background and it stop automatically when the program is not running. We can make a thread Daemon by Thread.IsBackground = true;
There are certain situations that we need to be careful with when using threads. If two threads try to access the same variable at the same time, we’ll have a problem. This can be very difficult to debug because they may not always do it at exactly the same time. To avoid the problem, we can lock a variable before accessing it. However, if the two threads lock the same variable at the same time, we will have a deadlock problem. To avoid dead lock we need to use the ‘lock’ keyword as shown below.
To synchronize one thread with other we can use events.
“ThreadState” property can be used to get detail of a thread. Thread can have one or a combination of status. ‘System.Threading.ThreadState’ enumeration has all the values to detect a state of thread.
It stands for Task Parallel Library. It is a set of public types and API’s in the System.Threading and System.Threading.Tasks namespaces. When we use threading API directly we need to take care of pooling, ensure that thread executes on multiple processors, data and task parallelism etc. When we use TPL all these things are taken care of so that we concentrate on the work rather than these lower level technical details.
It simplifies the following things from development aspect:
· Thread pooling is taken care internally.
· It ensures that threads will execute on multiple processors.
· Cancellation of threads made easier.
· State management simplified as compared to core threading.
It’s all components exists in the below namespace.
To invoke a thread using it we need to use the below syntax. We need to create the object of task object to invoke a thread.
Task tsk = new Task(Mytask);