using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
namespace SemaphoreTest
{
class SemaphoreTest
{
static object m_lock = new object();
static void Main(string[] args)
{
Semaphore s = new Semaphore(0, 1024);
Thread t1 = new Thread(new ParameterizedThreadStart(Thread1));
t1.IsBackground = true;
t1.Start(s);
Thread t2 = new Thread(new ParameterizedThreadStart(Thread2));
t2.IsBackground = true;
t2.Start(s);
Console.ReadLine();
}
static void Thread1(object obj)
{
Semaphore s = obj as Semaphore;
while (true)
{
s.WaitOne();
WriteLine("Get One Semaphore");
Thread.Sleep(100);
}
}
static void Thread2(object obj)
{
Semaphore s = obj as Semaphore;
while (true)
{
s.Release();
WriteLine("Release One Semaphore");
Thread.Sleep(1000);
}
}
static void WriteLine(string msg)
{
lock (m_lock)
{
Console.WriteLine(msg);
}
}
}
}
C#多线程之信号量
最新推荐文章于 2024-08-13 13:10:22 发布