/** * Return a {@link BalancingThreadPoolExecutor} with the given * {@link ThreadPoolExecutor}, target utilization, smoothing weight, and * balance after values. * * @param tpe the underlying executor to use for this instance * @param targetUtilization a float between 0.0 and 1.0 representing the * percentage of the total CPU time to be used by * this pool * @param smoothingWeight smooth out the averages of the CPU and wait time * over time such that tasks aren't too heavily * skewed with old or spiking data * @param balanceAfter balance the thread pool after this many tasks * have run */ public static BalancingThreadPoolExecutor newBalancingThreadPoolExecutor(ThreadPoolExecutor tpe, float targetUtilization, float smoothingWeight, int balanceAfter) { ThreadProfiler tp = new MXBeanThreadProfiler(); return new BalancingThreadPoolExecutor(tpe, tp, targetUtilization, smoothingWeight, balanceAfter); } }
Thread thisThread = Thread.currentThread(); long threadId = thisThread.getId(); long startTime = threadProfiler.getThreadWaitTime(threadId); long startCpu = threadProfiler.getThreadCpuTime(threadId); try { command.run(); } finally { Tracking tracking = liveThreads.get(thisThread); long totalCpuTime = threadProfiler.getThreadCpuTime(threadId) - startCpu; long totalTime = threadProfiler.getThreadWaitTime(threadId) - startTime; if(tracking == null) { tracking = new Tracking(); tracking.avgTotalTime = totalTime; tracking.avgCpuTime = totalCpuTime;
/** * Return a capped {@link BalancingThreadPoolExecutor} with the given * maximum number of threads, target utilization, smoothing weight, and * balance after values. * * @param maxThreads maximum number of threads to use * @param targetUtilization a float between 0.0 and 1.0 representing the * percentage of the total CPU time to be used by * this pool * @param smoothingWeight smooth out the averages of the CPU and wait time * over time such that tasks aren't too heavily * skewed with old or spiking data * @param balanceAfter balance the thread pool after this many tasks * have run */ public static BalancingThreadPoolExecutor newBalancingThreadPoolExecutor(int maxThreads, float targetUtilization, float smoothingWeight, int balanceAfter) { ThreadPoolExecutor tpe = new ThreadPoolExecutor(1, maxThreads, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new CallerBlocksPolicy()); return newBalancingThreadPoolExecutor(tpe, targetUtilization, smoothingWeight, balanceAfter); }
if(!isTerminated()) { Set<Map.Entry<Thread, Tracking>> threads = liveThreads.entrySet(); long liveAvgTimeTotal = 0;
configuration.getInt(QSConfig.PROP_WORKER_POOL_MAX), 1, TimeUnit.MINUTES, new SynchronousQueue<Runnable>(), new CallerBlocksPolicy() ), configuration.getFloat(QSConfig.PROP_WORKER_POOL_UTILIZATION), DEFAULT_SMOOTHING_WEIGHT, DEFAULT_BALANCE_AFTER