Webeks.net - freelance programming
freelance programming - php, Joomla, Zend ...
Home :: Articles :: Programming :: Programming :: Synchronize method in dotNet c#

Synchronize method in dotNet c#

Written by Miha

 

public Boolean Check()
{
if(quantity>buyQuantity)
{
decreaseQuantity(buyQuantity);
}
}

As both users access at the same time, condition will be for both true and program will decrease quantity. Resulting in overselling our available stock.

When we use synchronized methods, only one thread at time is possible. Program will carry out request of user one, other user request will wait. After first request is carried out, second request will be handled.

using System.Runtime.CompilerServices;

[MethodImpl(MethodImplOptions.Synchronized)]
public Boolean Check()
{
if(quantity>buyQuantity)
{
decreaseQuantity(buyQuantity);
}
}

blog comments powered by Disqus