List of usage examples for java.util.concurrent ThreadPoolExecutor ThreadPoolExecutor
public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTime, TimeUnit unit, BlockingQueue<Runnable> workQueue, RejectedExecutionHandler handler)
From source file:Main.java
public static ThreadPoolExecutor newSingleThreadExecutor(ThreadFactory threadFactory) { return new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), threadFactory);/* w ww .ja v a 2 s.c om*/ }
From source file:Main.java
public static ExecutorService newFixedThreadPool(int threadSize) { if (threadSize <= 0) { throw new IllegalArgumentException("ThreadSize must be greater than 0!"); }// w w w. ja va 2s . c o m if (threadSize == 1) { return MoreExecutors.sameThreadExecutor(); } return new ThreadPoolExecutor(threadSize - 1, threadSize - 1, 0L, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>(), new ThreadPoolExecutor.CallerRunsPolicy()); }
From source file:Main.java
public static ThreadPoolExecutor newThreadPool(int maximumNumOfThreads, String poolName) { BlockingQueue<Runnable> workQueue = new LinkedBlockingQueue<>(CAPACITY); ThreadPoolExecutor threadPoolExecutor = new ThreadPoolExecutor(maximumNumOfThreads, maximumNumOfThreads, 1, TimeUnit.SECONDS, workQueue, createFactory(poolName)); threadPoolExecutor.allowCoreThreadTimeOut(true); return threadPoolExecutor; }
From source file:Main.java
public static ThreadPoolExecutor newFixedThreadPool(int corePoolSize, BlockingQueue<Runnable> queue, ThreadFactory threadFactory) { return new ThreadPoolExecutor(corePoolSize, corePoolSize, 0, TimeUnit.MILLISECONDS, queue, threadFactory); }
From source file:Main.java
/** * @param threadToCpuRatio - for example, assuming you have 2 CPUs and setting a threadToCpuRation to 3, the result will be a pool with 6 working threads. * @return an {@link ExecutorService} with defined amount of worker thread for each CPUm A {@link SynchronousQueue} and a {@link ThreadPoolExecutor.CallerRunsPolicy} *//*from w w w. j a v a2 s .c om*/ public static ExecutorService getQueuedThreadPool(double threadToCpuRatio, int queueCapacity) { int workingThreads = Double.valueOf(NUM_CPU * threadToCpuRatio).intValue(); return new ThreadPoolExecutor(workingThreads, workingThreads, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(queueCapacity), new ThreadPoolExecutor.CallerRunsPolicy()); }
From source file:Main.java
/** * Create a cached thread pool whose max number of threads is `maxThreadNumber`. Thread names * are formatted as prefix-ID, where ID is a unique, sequentially assigned integer. *//*from ww w. ja va 2 s . co m*/ public static ThreadPoolExecutor newDaemonCachedThreadPool(String prefix, int maxThreadNumber) { ThreadFactory threadFactory = namedThreadFactory(prefix); return new ThreadPoolExecutor(0, maxThreadNumber, 60L, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), threadFactory); }
From source file:Main.java
public static ThreadPoolExecutor createExecutor(final String name, int count, int keepAlive, final boolean isDaemon) { ThreadPoolExecutor exe = new ThreadPoolExecutor(count, count, keepAlive, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new ThreadFactory() { private int threadNum = 1; public Thread newThread(Runnable r) { Thread t = new Thread(r, name + (threadNum++)); t.setDaemon(isDaemon); return t; }/*from w ww .ja va 2s . c om*/ }); // if (keepAlive > 0) { // // FIXME JDK 1.7 ? // if (SystemUtils.IS_JAVA_1_5 == false) { // try { // exe.allowCoreThreadTimeOut(true); // } catch(Throwable t) { } // } // } return exe; }
From source file:Main.java
/** * @param threadToCpuRatio - for example, assuming you have 2 CPUs and setting a threadToCpuRation to 3, the result will be a pool with 6 working threads. * @return an {@link ExecutorService} with defined amount of worker thread for each CPUm A {@link SynchronousQueue} and a {@link ThreadPoolExecutor.CallerRunsPolicy} */// w ww. j ava2 s . c om public static ExecutorService getTightThreadPool(double threadToCpuRatio) { int workingThreads = Double.valueOf(NUM_CPU * threadToCpuRatio).intValue(); return new ThreadPoolExecutor(workingThreads, workingThreads, 60, TimeUnit.SECONDS, new SynchronousQueue<Runnable>(true), new ThreadPoolExecutor.CallerRunsPolicy()); }
From source file:org.caffinitas.ohc.benchmark.BenchmarkOHC.java
public static void main(String[] args) throws Exception { Locale.setDefault(Locale.ENGLISH); Locale.setDefault(Locale.Category.FORMAT, Locale.ENGLISH); try {//ww w. ja v a2 s . co m CommandLine cmd = parseArguments(args); String[] warmUp = cmd.getOptionValue(WARM_UP, "15,5").split(","); int warmUpSecs = Integer.parseInt(warmUp[0]); int coldSleepSecs = Integer.parseInt(warmUp[1]); int duration = Integer.parseInt(cmd.getOptionValue(DURATION, "60")); int cores = Runtime.getRuntime().availableProcessors(); if (cores >= 8) cores -= 2; else if (cores > 2) cores--; int threads = Integer.parseInt(cmd.getOptionValue(THREADS, Integer.toString(cores))); long capacity = Long.parseLong(cmd.getOptionValue(CAPACITY, "" + (1024 * 1024 * 1024))); int hashTableSize = Integer.parseInt(cmd.getOptionValue(HASH_TABLE_SIZE, "0")); int segmentCount = Integer.parseInt(cmd.getOptionValue(SEGMENT_COUNT, "0")); float loadFactor = Float.parseFloat(cmd.getOptionValue(LOAD_FACTOR, "0")); int keyLen = Integer.parseInt(cmd.getOptionValue(KEY_LEN, "0")); int chunkSize = Integer.parseInt(cmd.getOptionValue(CHUNK_SIZE, "-1")); int fixedKeySize = Integer.parseInt(cmd.getOptionValue(FIXED_KEY_SIZE, "-1")); int fixedValueSize = Integer.parseInt(cmd.getOptionValue(FIXED_VALUE_SIZE, "-1")); int maxEntrySize = Integer.parseInt(cmd.getOptionValue(MAX_ENTRY_SIZE, "-1")); boolean unlocked = Boolean.parseBoolean(cmd.getOptionValue(UNLOCKED, "false")); HashAlgorithm hashMode = HashAlgorithm.valueOf(cmd.getOptionValue(HASH_MODE, "MURMUR3")); boolean bucketHistogram = Boolean.parseBoolean(cmd.getOptionValue(BUCKET_HISTOGRAM, "false")); double readWriteRatio = Double.parseDouble(cmd.getOptionValue(READ_WRITE_RATIO, ".5")); Driver[] drivers = new Driver[threads]; Random rnd = new Random(); String readKeyDistStr = cmd.getOptionValue(READ_KEY_DIST, DEFAULT_KEY_DIST); String writeKeyDistStr = cmd.getOptionValue(WRITE_KEY_DIST, DEFAULT_KEY_DIST); String valueSizeDistStr = cmd.getOptionValue(VALUE_SIZE_DIST, DEFAULT_VALUE_SIZE_DIST); DistributionFactory readKeyDist = OptionDistribution.get(readKeyDistStr); DistributionFactory writeKeyDist = OptionDistribution.get(writeKeyDistStr); DistributionFactory valueSizeDist = OptionDistribution.get(valueSizeDistStr); for (int i = 0; i < threads; i++) { drivers[i] = new Driver(readKeyDist.get(), writeKeyDist.get(), valueSizeDist.get(), readWriteRatio, rnd.nextLong()); } printMessage("Initializing OHC cache..."); OHCacheBuilder<Long, byte[]> builder = OHCacheBuilder.<Long, byte[]>newBuilder() .keySerializer( keyLen <= 0 ? BenchmarkUtils.longSerializer : new BenchmarkUtils.KeySerializer(keyLen)) .valueSerializer(BenchmarkUtils.serializer).capacity(capacity); if (cmd.hasOption(LOAD_FACTOR)) builder.loadFactor(loadFactor); if (cmd.hasOption(SEGMENT_COUNT)) builder.segmentCount(segmentCount); if (cmd.hasOption(HASH_TABLE_SIZE)) builder.hashTableSize(hashTableSize); if (cmd.hasOption(CHUNK_SIZE)) builder.chunkSize(chunkSize); if (cmd.hasOption(MAX_ENTRY_SIZE)) builder.maxEntrySize(maxEntrySize); if (cmd.hasOption(UNLOCKED)) builder.unlocked(unlocked); if (cmd.hasOption(HASH_MODE)) builder.hashMode(hashMode); if (cmd.hasOption(FIXED_KEY_SIZE)) builder.fixedEntrySize(fixedKeySize, fixedValueSize); Shared.cache = builder.build(); printMessage("Cache configuration: instance : %s%n" + " hash-table-size: %d%n" + " load-factor : %.3f%n" + " segments : %d%n" + " capacity : %d%n", Shared.cache, Shared.cache.hashTableSizes()[0], Shared.cache.loadFactor(), Shared.cache.segments(), Shared.cache.capacity()); String csvFileName = cmd.getOptionValue(CSV, null); PrintStream csv = null; if (csvFileName != null) { File csvFile = new File(csvFileName); csv = new PrintStream(new FileOutputStream(csvFile)); csv.println("# OHC benchmark - http://github.com/snazy/ohc"); csv.println("# "); csv.printf("# started on %s (%s)%n", InetAddress.getLocalHost().getHostName(), InetAddress.getLocalHost().getHostAddress()); csv.println("# "); csv.printf("# Warum-up/sleep seconds: %d / %d%n", warmUpSecs, coldSleepSecs); csv.printf("# Duration: %d seconds%n", duration); csv.printf("# Threads: %d%n", threads); csv.printf("# Capacity: %d bytes%n", capacity); csv.printf("# Read/Write Ratio: %f%n", readWriteRatio); csv.printf("# Segment Count: %d%n", segmentCount); csv.printf("# Hash table size: %d%n", hashTableSize); csv.printf("# Load Factor: %f%n", loadFactor); csv.printf("# Additional key len: %d%n", keyLen); csv.printf("# Read key distribution: '%s'%n", readKeyDistStr); csv.printf("# Write key distribution: '%s'%n", writeKeyDistStr); csv.printf("# Value size distribution: '%s'%n", valueSizeDistStr); csv.printf("# Type: %s%n", Shared.cache.getClass().getName()); csv.println("# "); csv.printf("# started at %s%n", new Date()); Properties props = System.getProperties(); csv.printf("# java.version: %s%n", props.get("java.version")); for (Map.Entry<Object, Object> e : props.entrySet()) { String k = (String) e.getKey(); if (k.startsWith("org.caffinitas.ohc.")) csv.printf("# %s: %s%n", k, e.getValue()); } csv.printf("# number of cores: %d%n", Runtime.getRuntime().availableProcessors()); csv.println("# "); csv.println("\"runtime\";" + "\"r_count\";" + "\"r_oneMinuteRate\";\"r_fiveMinuteRate\";\"r_fifteenMinuteRate\";\"r_meanRate\";" + "\"r_snapMin\";\"r_snapMax\";\"r_snapMean\";\"r_snapStdDev\";" + "\"r_snap75\";\"r_snap95\";\"r_snap98\";\"r_snap99\";\"r_snap999\";\"r_snapMedian\";" + "\"w_count\";" + "\"w_oneMinuteRate\";\"w_fiveMinuteRate\";\"w_fifteenMinuteRate\";\"w_meanRate\";" + "\"w_snapMin\";\"w_snapMax\";\"w_snapMean\";\"w_snapStdDev\";" + "\"w_snap75\";\"w_snap95\";\"w_snap98\";\"w_snap99\";\"w_snap999\";\"w_snapMedian\""); } printMessage( "Starting benchmark with%n" + " threads : %d%n" + " warm-up-secs: %d%n" + " idle-secs : %d%n" + " runtime-secs: %d%n", threads, warmUpSecs, coldSleepSecs, duration); ThreadPoolExecutor main = new ThreadPoolExecutor(threads, threads, coldSleepSecs + 1, TimeUnit.SECONDS, new LinkedBlockingQueue<Runnable>(), new ThreadFactory() { volatile int threadNo; public Thread newThread(Runnable r) { return new Thread(r, "driver-main-" + threadNo++); } }); main.prestartAllCoreThreads(); // warm up if (warmUpSecs > 0) { printMessage("Start warm-up..."); runFor(warmUpSecs, main, drivers, bucketHistogram, csv); printMessage(""); logMemoryUse(); if (csv != null) csv.println("# warm up complete"); } // cold sleep if (coldSleepSecs > 0) { printMessage("Warm up complete, sleep for %d seconds...", coldSleepSecs); Thread.sleep(coldSleepSecs * 1000L); } // benchmark printMessage("Start benchmark..."); runFor(duration, main, drivers, bucketHistogram, csv); printMessage(""); logMemoryUse(); if (csv != null) csv.println("# benchmark complete"); // finish if (csv != null) csv.close(); System.exit(0); } catch (Throwable t) { t.printStackTrace(); System.exit(1); } }
From source file:Main.java
/** * Creates a fixed priority thread pool. *//*from ww w . ja v a 2 s .c o m*/ public static ExecutorService newPriorityThreadPool(String name, int numThreads) { return new ThreadPoolExecutor(numThreads, numThreads, 1, TimeUnit.SECONDS, new PriorityBlockingQueue<Runnable>(numThreads), newNamedThreadFactory(name)); }