Advertisement
Guest User

Generic Pool Test

a guest
Apr 3rd, 2010
2,866
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.63 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace Pooling
  5. {
  6.     class Program
  7.     {
  8.         static int PoolSize = 5;
  9.  
  10.         static void Main(string[] args)
  11.         {
  12.             using (Pool<IFoo> pool = new Pool<IFoo>(PoolSize, p => new PooledFoo(p),
  13.                 LoadingMode.Lazy, AccessMode.Circular))
  14.             {
  15.                 using (ManualResetEvent finishedEvent = new ManualResetEvent(false))
  16.                 {
  17.                     int remaining = 10;
  18.                     for (int i = 0; i < 10; i++)
  19.                     {
  20.                         int q = i;
  21.                         ThreadPool.QueueUserWorkItem(s =>
  22.                         {
  23.                             Console.WriteLine("Thread started: {0}", q);
  24.                             for (int j = 0; j < 50; j++)
  25.                             {
  26.                                 using (IFoo foo = pool.Acquire())
  27.                                 using (IFoo foo2 = pool.Acquire())
  28.                                 {
  29.                                     foo.Test();
  30.                                     foo2.Test();
  31.                                 }
  32.                             }
  33.                             if (Interlocked.Decrement(ref remaining) == 0)
  34.                             {
  35.                                 finishedEvent.Set();
  36.                             }
  37.                         });
  38.                     }
  39.                     finishedEvent.WaitOne();
  40.                 }
  41.                 Console.WriteLine("Threaded partial load test finished.");
  42.                 Console.WriteLine();
  43.             }
  44.             Console.ReadLine();
  45.         }
  46.     }
  47. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement