using System.Threading;
public class Program
{
static object ball = new object();
public static void Main()
{
Thread threadPing = new Thread(ThreadPingProc);
Thread threadPong = new Thread(ThreadPongProc);
threadPing.Start(); threadPong.Start();
}
static void ThreadPongProc()
{
System.Console.WriteLine("ThreadPong: Hello!");
lock (ball)
for (int i = 0; i < 5; i++)
{
System.Console.WriteLine("ThreadPong: Pong ");
Monitor.Pulse(ball);
Monitor.Wait(ball);
}
System.Console.WriteLine("ThreadPong: Bye!");
}
static void ThreadPingProc()
{
System.Console.WriteLine("ThreadPing: Hello!");
lock (ball)
for (int i = 0; i < 5; i++)
{
System.Console.WriteLine("ThreadPing: Ping ");
Monitor.Pulse(ball);
Monitor.Wait(ball);
}
System.Console.WriteLine("ThreadPing: Bye!");
}
}
乒乓球锁:Lock an Object:Switch Different Functions
最新推荐文章于 2024-09-14 21:24:31 发布
1万+

被折叠的 条评论
为什么被折叠?



