Synchronize method in dotNet c#
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);
}
}
| < Prev | Next > |
|---|