Golgappa.net | Golgappa.org | BagIndia.net | BodyIndia.Com | CabIndia.net | CarsBikes.net | CarsBikes.org | CashIndia.net | ConsumerIndia.net | CookingIndia.net | DataIndia.net | DealIndia.net | EmailIndia.net | FirstTablet.com | FirstTourist.com | ForsaleIndia.net | IndiaBody.Com | IndiaCab.net | IndiaCash.net | IndiaModel.net | KidForum.net | OfficeIndia.net | PaysIndia.com | RestaurantIndia.net | RestaurantsIndia.net | SaleForum.net | SellForum.net | SoldIndia.com | StarIndia.net | TomatoCab.com | TomatoCabs.com | TownIndia.com
Interested to Buy Any Domain ? << Click Here >> for more details...


Write a standard lock() plus double check to create a
critical section around a variable access?



Write a standard lock() plus double check to create a critical section around a variable access?..

Answer / debmalya kundu

using System;
using System.Text;

using System.Threading;

namespace thread01

{

public class Counter

{

private int _count=0;

private int _even=0;

public int Count { get { return _count; } }

public int EvenCount { get { return _even; } }




private Object theLock = new Object();



public void UpdateCount()

{

lock (theLock)

{

_count = _count + 1;

if (Count % 2 == 0) // An even number

{

_even += 1;


}

}

}

}

class Program

{

static void Main(string[] args)

{

Counter count = new Counter();

ParameterizedThreadStart starter = new
ParameterizedThreadStart(Program.UpdateCount);

Thread[] threads = new Thread[10];


for (int x = 0; x < 10; ++x)

{

threads[x] = new Thread(starter);

threads[x].Start(count);

}

for (int y = 0; y < 10; ++y)

{

threads[y].Join();

}

Console.WriteLine("Total: {0} - Even: {1}",
count.Count,count.EvenCount);

Console.ReadKey();

Console.ReadKey();

}

static void UpdateCount(object param)

{

Counter count = (Counter)param;

for (int z = 1; z <= 100000; ++z)

{

count.UpdateCount();

}

}

}

}

Is This Answer Correct ?    3 Yes 0 No

Post New Answer

More ASP.NET Interview Questions

How asynchronous call can be implemented using delegates?

0 Answers  


Explain about the Class view window?

0 Answers  


What is asp.net localization?

0 Answers  


How do we assign page-specific attributes?

0 Answers  


What is OSI layer? Explain different layers.

0 Answers   MCN Solutions,


State Management (viewstate, session etc)

2 Answers   Syntel,


What is the difference between a multi-layer and multi-tier applications?

0 Answers   Microsoft,


if i have 1000 records and i want to access 20 ata time from SQL server, what will be the query?

9 Answers   Mind Tree,


Can we have a web application running without web.config file?

0 Answers  


Difference between asp and asp.net ?

4 Answers   Accenture, BirlaSoft, TCS,


What are the different types of validation controls in asp.net?

0 Answers  


What are the type of session in Asp.Net

3 Answers  


Categories