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 |
What is the difference between table and query?
To make Cache item dependent on a file,directory or other cached item ,you should create an instance of which clause?
what is caching,session? when & what is used mostly in which situtations, how they r implemented in real time? tell with example?
How much is the pay-for-use service if I chose not to use microsoft-sponsored advertising?
i have 3+ exp in .net? i am going interview now but they asked me do you know any TOOL? which tool will i study please refer me?
Caching techniques in .NET?
What are the uses of reflection?
Define cookie.
What is http response header?
Explain the difference between globalization and localization techniques
Will the asp.net validators run in server side or client side?
what is diffgram ?
Visual Basic (800)
C Sharp (3816)
ASP.NET (3180)
VB.NET (461)
COM+ (79)
ADO.NET (717)
IIS (369)
MTS (11)
Crystal Reports (81)
BizTalk (89)
Dot Net (2435)
Exchange Server (362)
SharePoint (720)
WCF (340)
MS Office Microsoft (6963)
LINQ Language-Integrated Query (317)
WPF (371)
TypeScript (144)
Microsoft Related AllOther (311)