Package org.jboss.cache.profiling.testinternals

Source Code of org.jboss.cache.profiling.testinternals.TaskRunner

package org.jboss.cache.profiling.testinternals;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;

/**
* Essentially a delegate to an ExecutorService, but a special one that is only used by perf tests so it can be ignored
* when profiling.
*/
public class TaskRunner
{
   ExecutorService exec;

   public TaskRunner(int numThreads)
   {
      final ThreadGroup tg = new ThreadGroup(Thread.currentThread().getThreadGroup(), "LoadGenerators");
      final AtomicInteger ai = new AtomicInteger(1);
      this.exec = Executors.newFixedThreadPool(numThreads, new ThreadFactory() {        
         public Thread newThread(Runnable r) {
            return new Thread(tg, r, "LoadGenerator-" + ai.getAndIncrement());
         }
      });
   }

   public void execute(Runnable r)
   {
      exec.execute(r);
   }

   public void stop() throws InterruptedException
   {
      exec.shutdown();
      while (!exec.awaitTermination(30, TimeUnit.SECONDS)) Thread.sleep(30);
   }
}
TOP

Related Classes of org.jboss.cache.profiling.testinternals.TaskRunner

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.