In multithreading programming, the word “thread-safety” is often used. Thread-Safety is relevant when resources are shared between threads. In other words, when there is a chance of two concurrent threads accessing a shared resource simultaneously. Examples may include multiple threads accessing a shared database, for instance, or updates to a set of system variables. This behavior is also known as a race condition. A race condition exists when a thread modifies a resource to an intermediate state, and then another thread attempts to access that resource and use it in the intermediate state.
Let’s understand it with an example as in the following:
- public class BankAccount
- {
- public int TotalAmount = 0;
- public void AddAmount(int amount)
- {
- TotalAmount += amount;
- }
- public void RemoveAmount(int amount)
- {
- TotalAmount -= amount;
- }
- }
Using locks as a solution to Race Condition:
The purpose of the lock statement is to prevent the same block of code from being run at the same time on different threads. You can only obtain a lock on an object that returns a reference. A value type cannot be locked with this approach.
The code between the curly braces define the scope of the lock statement. When execution reaches the lock statement, if the specified object is currently locked, the thread requesting the lock is blocked and the code is suspended at this point. When the thread that currently holds the lock reaches the closing curly brace of the lock statement, the lock is released, enabling the blocked thread to acquire the lock itself and continue.
- lock (MyObject)
- {
- // Insert code that affects MyObject.
- }
Using locks in this manner is safe but can degrade performance to a great extent. Performance may be worsened with a program with many different running threads. If each thread requires access to a specific object and must wait to obtain an exclusive lock on that object before executing, the threads will all cease executing and back up behind one another, causing poor performance. Due to this, locks are recommended when code is executed as a unit.
On the other hand, a deadlock might occur when multiple threads wait for each other to release shared resources. For example, Thread 1 might hold a lock on resource A and is waiting for resource B. Thread 2, on the other hand, might have a lock on resource B and awaits resource A. In such a case, neither of the threads will be allowed to proceed. Deadlock situations can only be avoided through careful programming.
Reference: Thread-Safe Components.

Santhakumar MunuswamyPosted Aug 22, 2015, 7:56 AM
Good Article. Thanks for sharing
Yashwanth MuthineniPosted Aug 22, 2015, 7:31 AM
Nice Share ..
Sandeep KumarPosted Aug 20, 2015, 5:19 AM
nice aricle :)
Ankit BansalPosted Aug 20, 2015, 1:44 AM
good one...
RakeshPosted Aug 19, 2015, 11:26 PM
Good one
Vipul MalhotraPosted Aug 19, 2015, 7:48 AM
nice
Nilesh JadavPosted Aug 19, 2015, 7:25 AM
Great Post
Sibeesh VenuPosted Aug 19, 2015, 6:53 AM
Nice Share
Gopi ChandPosted Aug 19, 2015, 6:00 AM
Great one
Vaikesh K PPosted Aug 19, 2015, 5:51 AM
Nice One
Gowtham KPosted Aug 19, 2015, 5:39 AM
Good One Thanks for Sharing
Karthikeyan KPosted Aug 19, 2015, 4:29 AM
Good one sir
Mohammed IbrahimPosted Aug 19, 2015, 4:29 AM
nice
Rajeesh MenothPosted Aug 19, 2015, 4:22 AM
Good One..