标签:mamicode ref color task 线程 console inter syn for
class Program { public static long flgVal = 0; static void Main(string[] args) { int n = 100; Task.Run(() => DaMi(n)); Task.Run(() => LaoShu(n)); Console.Read(); } public async static Task LaoShu(int n) { for (int i = 0; i < n; i++) { while (true) { if (Interlocked.Read(ref flgVal) == 0) { break; } } Console.Write("老鼠"); Interlocked.Increment(ref flgVal); } } public async static Task DaMi(int n) { for (int i = 0; i < n; i++) { while (true) { if (Interlocked.Read(ref flgVal) == 1) { break; } } Console.Write("爱大米 "); Interlocked.Decrement(ref flgVal); } } }
标签:mamicode ref color task 线程 console inter syn for
原文地址:https://www.cnblogs.com/puzi0315/p/14885000.html