Ejemplo n.º 1
0
 /// <summary>
 /// Creates a new scheduled thread pool of a given size with the given name, or returns an
 /// existing thread pool if one was already created with the same name.
 /// </summary>
 /// <param name="poolSize">the number of threads to create</param>
 /// <param name="name">the name of the pool</param>
 /// <returns>a new <seealso cref="IScheduledExecutorService" /></returns>
 public IScheduledExecutorService NewScheduledThreadPool(int poolSize, string name)
 {
     var existing = threadPools.Get(name);
     if (IsValidExecutor(existing))
     {
         return existing;
     }
     else
     {
         // We lock here because executors are expensive to create. So
         // instead of just doing the usual putIfAbsent dance, we lock the
         // damn thing, check to see if anyone else put a thread pool in
         // there while we weren't watching.
         lock (this)
         {
             var lastChance = threadPools.Get(name);
             if (IsValidExecutor(lastChance))
             {
                 return lastChance;
             }
             else
             {
                 var service = Executors.DefaultScheduledExecutorService();
                 threadPools.Put(name, service);
                 return service;
             }
         }
     }
 }