Using System;
Using System. Collections. Generic;
Using System. Text;
Using System. Threading;
Namespace ConsoleCsharp
{
Class Proxy
{
System. Threading. Thread worker;
Queue <WaitCallback> queue;
// List <WaitCallback> list;
System. Threading. Semaphore sm = new Semaphore (1, 1 );
Public Proxy ()
{
Queue = new Queue <WaitCallback> ();
Worker = new System. Threading. Thread (this. run );
Worker. Name = "worker ";
Worker. Start ();
}
Public void run ()
{
For (;;)
{
WaitCallback wc = null;
Sm. WaitOne ();
If (queue. Count> = 1)
{
Wc = queue. Dequeue ();
}
Sm. Release ();
If (wc! = Null)
{
Wc. Invoke (null );
}
}
}
Public void handle (WaitCallback wc)
{
Sm. WaitOne ();
Queue. Enqueue (wc );
Sm. Release ();
}
}
Class Caller
{
Proxy pro;
Public Caller ()
{
Pro = new Proxy ();
}
Public void doFunc (int I)
{
Pro. handle (delegate {
System. Console. Out. WriteLine (Thread. CurrentThread. Name + ":" + I );
});
}
}
Class Program
{
Static void Main (string [] args)
{
Caller c = new Caller ();
For (int I = 0; I <100; ++ I)
{
C. doFunc (I );
}
While (true)
Thread. Sleep (100 );
}
}
}
Author: jingshishengxu