Using Synchronization in Threads

By: Jesse Miller

In a multithreaded application, when threads need to share thread data with each other, the application should ensure that one thread does not change the data used by the other thread. Data corruption might occur if there are two threads, one that reads your salary from a file and another thread tries to update the salary.

C sharp enables you to coordinate the actions of multiple threads by using synchronized methods synchronized statements.

Synchronization of threads ensures that if two or more threads need to access a shared resource then that resource is used by only one thread at a time. Synchronize code using the synchronized keyword. Only one synchronized method for an object at any given time. Synchronization is based on the concept of monitoring. A monitor is an object that is used as a lock to the data members and methods of a class.

All objects and classes are associated with the monitor and only one thread can own the monitor at a given time. To enter an object`s monitor, a method has to be called which is modified with the synchronized keyword.

The monitor controls the way in which synchronized methods access an object or a class. When a thread acquires a lock, it is said to have entered the monitor. The monitor ensures that only one thread has access to the resources at any given time.
When a thread is within a synchronized method, all the other threads that try to call it on the same instance have to wait.

During the execution of a synchronized method, the object is locked so that no other synchronized method can be invoked. The monitor is automatically released when the method completes its execution. The monitor can also be released when the synchronized methods executes the Wait () method.

When a thread calls the Wait () method, it temporarily releases the locks that it holds. In addition, the thread stops running and is added to the list of waiting threads for that object.

Quick Note: Taking the Nonsense out of looking for the right spyware remover

If you really want to take the work out of looking for that right Spyware Protection from a go to the Internet and get a or a Free
Download, In order to prevent your vital information from being ripped from your computer get your Remover Today.

The Sysytem.Monitor class enables you to serialize the access to blocks of code by means of locks and signals. The other way to lock code is by using the c sharp lock statement. Although the c sharp lock statement does not support the full array of features found in the Monitor class, it enables you to obtain and release a monitor lock. Specify the lock statement.

Computers
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 
 • 

» More on Computers