下面的代码返回以下输出:
(('GB', 'United Kingdom'), ('AU', 'Australia'), ('AT', 'Austria'), ('BE', 'Belgium'), ('CA', 'Canada'), ('DK', 'Denmark'), ('FI', 'Finland'), ('FR', 'France'), ('DE', 'Germany'), ('HK', 'Hong Kong'), ('IE', 'Ireland'), ('IT', 'Italy'), ('LU', 'Luxembourg'), ('NL', 'Netherlands'), ('NZ', 'New Zealand'), ('NO', 'Norway'), ('PT', 'Portugal'), ('SG', 'Singapore'), ('ES', 'Spain'), ('SE', 'Sweden'), ('CH', 'Switzerland'), ('US', 'United States'))
只需添加int j = i,它将输出完全更改为我期望的值,这是达到信号量限制的正确响应:
Semaphore sema = new Semaphore(5,5,"sema_1");
for (int i = 0; i < 10; i++)
{
ThreadPool.QueueUserWorkItem(delegate(object state)
{
sema.WaitOne();
Console.WriteLine(" = " + i);
sema.Release();
});
}