闽公网安备 35020302035485号





static void Main(string[] args)
{
int i = 0;
while (true)
{
Console.WriteLine(++i);
Thread.Sleep(1000);
}
}
我们在目标主机运行起来,然后在本地利用源码进行远程调试,并且加上一个断点。发现断点进来了,并且程序不再打印,则调试成功。
private static object _lock = new object();
private static object _lock_1 = new object();
static void Main(string[] args)
{
var task1 = Task.Run(DeadLock);
var task2 = Task.Run(DeadLock_1);
Task.WaitAll(task1,task2);
Console.WriteLine("Hello, World!");
}
private static void DeadLock()
{
lock (_lock)
{
Thread.Sleep(200);
lock (_lock_1)
{
Console.WriteLine("DeadLock");
}
}
}
private static void DeadLock_1()
{
lock (_lock_1)
{
Thread.Sleep(200);
lock (_lock)
{
Console.WriteLine("DeadLock_1");
}
}
}
2.运行程序之后,将程序中断

static async Task Main(string[] args)
{
await MultiRunning();
SomeThing();
Console.WriteLine("Hello, World!");
}
//启动20个线程
static async Task MultiRunning()
{
Thread[] tasks = new Thread[20];
foreach (var index in Enumerable.Range(0, tasks.Length))
{
tasks[index] = new Thread(() =>
{
while (true)
{
int a = 1;
Thread.Sleep(200);
}
});
}
int i = 0;
await Parallel.ForEachAsync(tasks, (task, source) =>
{
i++;
task.Start();
return ValueTask.CompletedTask;
});
Console.WriteLine("执行了" + i);
}
static void SomeThing()
{
while (true)
{
Console.WriteLine("I AM Alive");
Thread.Sleep(200);
}
}
我们使用Performance Profiler对启动项目的CPU进行监控。

