using System;
using System.Threading;
class Program
{
private static int i = 0;
private static volatile int j = 0;
static void Main(string[] args)
{
Timer timer = new Timer(TimeCallback, null, 1000, 60 * 1000);
Console.ReadLine();
}
private static void TimeCallback(object state)
{
i++;
if (i > 10000 * 10000)
{
i = 0;
}
j++;
if (j > 10000 * 10000)
{
j = 0;
}
Console.WriteLine(i + "," + j);
}
}
In above code, TimeCallback run very fast, it can't run concurrently during the timer's interval. So I Know it is atomic, no need to lock()
.
My question is, does i
can be non volatile, or it must be volatile like j
.
If i
must be volatile, how does akka actor field can be non volatile?