Just a quick .NET puzzle. Does this application ever throw that ApplicationException? If so, why?
using System;
using System.Threading;
class Program
{
static long Num = 0;
static void Main(string[] args)
{
Thread t1 = new Thread(ModifyNum);
t1.Start();
while (true)
{
long k = Num;
if (k != -1 && k != 0) throw new ApplicationException(
"k is not -1 or 0. It is " + k.ToString());
}
}
static void ModifyNum()
{
while (true)
{
if (Num == -1) Num = 0;
else Num = -1;
}
}
}
Stuck? Then here's a hint: if you change Num from a long to an int, it never throws the exception.
Load comments