class Program
{
private static int Count = 0;
private static object lock_obj = new object();
static void Main(string[] args)
{
for (int i = 0; i < 100; i++)
{
Thread t = new Thread(Foo);
t.Start();
}
Console.ReadLine();
}
public static void Foo()
{
Thread.Sleep(1000);
//加锁输出的才是有序的, Monitor.Enter和lock功能一样的
Monitor.Enter(lock_obj);
Console.WriteLine(Count++);
Monitor.Exit(lock_obj);
}
}
C# 简单的 多线程 (1)
最新推荐文章于 2024-08-30 14:17:16 发布