Monitor Pool
using System;
using System.Threading;
class MainClass
{
private const int threads = 4;
private const int workitems = 50;
private static Object locker = new Object();
static void Worker()
{
while( true )
{
lock( locker )
{
Monitor.Wait( locker );
}
System.Console.WriteLine( "{0} doing work", Thread.CurrentThread.Name );
Thread.Sleep( 100 );
}
}
[STAThread]
static void Main(string[] args)
{
Thread[] t = new Thread[ threads ];
for( int k = 0; k < threads; k++ )
{
t[ k ] = new Thread( new ThreadStart( Worker ) );
t[ k ].Name = "Worker " + k;
t[ k ].IsBackground = true;
t[ k ].Start();
}
for( int i = 0; i < workitems; i ++ )
{
Thread.Sleep( 1000 );
lock( locker )
{
Monitor.Pulse( locker );
}
}
}
}
|
HTML code for linking to this page:
Related in same category :
-
-
-
-
-
-
-
-
|