110,534
社区成员
发帖
与我相关
我的任务
分享
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Diagnostics;
using System.Threading;
namespace DataTest123
{
class Program
{
static void Main(string[] args)
{
bool delay = false;
double a = 0;
var sw = new Stopwatch();
while (true)
{
if(!delay)
a += 0.2;
Console.WriteLine(a);
if (a > 10)
{
sw.Start();
}
if (sw.ElapsedMilliseconds >= 3000)
{
delay = false;
sw.Stop();
sw.Reset();
}
Console.WriteLine("hello world");
}
}
}
}
SpinLock sl = new SpinLock();
ManualResetEventSlim mre1 = new ManualResetEventSlim(false);
ManualResetEventSlim mre2 = new ManualResetEventSlim(false);
double a = 0;
for (int i = 0; i < 20; i++)
{
Task.Run(() =>
{
bool gotLock = false;
try
{
gotLock = false;
sl.Enter(ref gotLock);
a += 0.2;
if (a >= 1 && !mre2.IsSet)
{
WriteLine($"a={a}");
mre2.Set();
mre1.Wait(3000);
mre1.Set();
mre2.Wait();
}
else
{
WriteLine($"a={a}");
WriteLine("->hello,world");
}
}
finally
{
if (gotLock)
sl.Exit();
}
Task.Delay(1000).Wait();
});
Task.Run(() =>
{
if (!mre1.IsSet)
{
mre2.Wait();
WriteLine("->继续 hello,world");
Task.Delay(1000).Wait();
}
});
}