Multi-hilo C # con cerraduras para resolver el problema de la competencia por los recursos
class Program
{
static object locker=new Object();
static int init = 100;
static void Main(string[] args)
{
for (int i = 1; i <= 5; i++)
{
ThreadStart ts = new ThreadStart(run);
Thread th = new Thread(ts);
th.Start();
}
Console.ReadLine();
}
static void run()
{
lock (locker) {
while (init > 0) {
init--;
Console.WriteLine("线程:"+Thread.CurrentThread.Name+",执行减1后的结果:"+init.ToString());
//Thread.Sleep(100);
}
}
}
}