1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
|
class Program
{
static int i = 0;
static void Main(string[] args)
{
Stopwatch sw = new Stopwatch();
sw.Start();
while (i < 100)
{
CreateThread();
}
sw.Stop();
Console.WriteLine(string.Format("Creation thread : {0} Ms", sw.ElapsedMilliseconds));
sw.Reset();
i = 0;
sw.Start();
while (i < 100)
{
UseThreadPool();
}
sw.Stop();
Console.WriteLine(string.Format("Utilistation du pool de thread : {0} Ms", sw.ElapsedMilliseconds));
Console.Read();
}
static void CreateThread()
{
Thread t = new Thread(Task);
t.Start();
}
static void UseThreadPool()
{
ThreadPool.QueueUserWorkItem(Task);
}
static void Task(object state)
{
i++;
}
} |