/** {@inheritDoc} */ @Override protected GridConfiguration getConfiguration(String gridName) throws Exception { GridConfiguration c = super.getConfiguration(gridName); c.setLocalHost(HOST); assert c.getClientConnectionConfiguration() == null; GridClientConnectionConfiguration clientCfg = new GridClientConnectionConfiguration(); clientCfg.setRestTcpPort(REST_TCP_PORT_BASE); GridSslContextFactory sslCtxFactory = sslContextFactory(); if (sslCtxFactory != null) { clientCfg.setRestTcpSslEnabled(true); clientCfg.setRestTcpSslContextFactory(sslCtxFactory); } c.setClientConnectionConfiguration(clientCfg); GridTcpDiscoverySpi disco = new GridTcpDiscoverySpi(); disco.setIpFinder(IP_FINDER); c.setDiscoverySpi(disco); TestCommunicationSpi spi = new TestCommunicationSpi(); spi.setLocalPort(GridTestUtils.getNextCommPort(getClass())); c.setCommunicationSpi(spi); c.setCacheConfiguration( cacheConfiguration(null), cacheConfiguration(PARTITIONED_CACHE_NAME), cacheConfiguration(REPLICATED_CACHE_NAME), cacheConfiguration(REPLICATED_ASYNC_CACHE_NAME)); ThreadPoolExecutor exec = new ThreadPoolExecutor(40, 40, 0, MILLISECONDS, new LinkedBlockingQueue<Runnable>()); exec.prestartAllCoreThreads(); c.setExecutorService(exec); c.setExecutorServiceShutdown(true); ThreadPoolExecutor sysExec = new ThreadPoolExecutor(40, 40, 0, MILLISECONDS, new LinkedBlockingQueue<Runnable>()); sysExec.prestartAllCoreThreads(); c.setSystemExecutorService(sysExec); c.setSystemExecutorServiceShutdown(true); return c; }
@Before public void setUp() throws Exception { writer = (ThreadPoolExecutor) Executors.newFixedThreadPool(writerThreadSize); reader = (ThreadPoolExecutor) Executors.newFixedThreadPool(readThreadSize); writer.prestartAllCoreThreads(); reader.prestartAllCoreThreads(); for (int i = 0; i < testMapSize; i++) { map.put(i, new AtomicReferenceTest()); } }
public void init() { int numThreads = Runtime.getRuntime().availableProcessors(); LOGGER.debug(QUERY_POOL_NAME + " size: {}", numThreads); /* - when first two args the same, get fixed size thread pool. - 3rd arg, keepAliveTime, ignored when !allowsCoreThreadTimeOut (the default); thus pass zero. - fixed (and arbitrarily) size blocking queue. - CswThreadFactory gives pool threads a name to ease debug. - tried arbitrarily large numThreads/queue-size, but did not see performance gain. - big queue + small pool minimizes CPU usage, OS resources, and context-switching overhead, but *can* lead to artificially low throughput. - todo: externalize config to support runtime tuning. */ queryExecutor = new ThreadPoolExecutor( numThreads, numThreads, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<Runnable>(BLOCKING_Q_INITIAL_SIZE), new CswThreadFactory(), new ThreadPoolExecutor.CallerRunsPolicy()); queryExecutor.prestartAllCoreThreads(); }
@Test public void testDeadLockWithNotify() throws Throwable { int size = 10; final ExecutorService es = new ThreadPoolExecutor( size * 2, Integer.MAX_VALUE, 30, TimeUnit.SECONDS, new SynchronousQueue()); ((ThreadPoolExecutor) es).prestartAllCoreThreads(); for (int i = 0; i < size; i++) es.execute(new WriteSpaceWithNotifyTask("WriteTask1-" + i, sp1, sp2)); for (int i = 0; i < size; i++) es.execute(new WriteSpaceWithNotifyTask("WriteTask2-" + i, sp2, sp1)); long stamp = System.currentTimeMillis(); while (((ThreadPoolExecutor) es).getActiveCount() > 0) { if (System.currentTimeMillis() - stamp < 10000) { ISOUtil.sleep(100); continue; } es.shutdownNow(); fail("Probably death-lock detected"); return; } printAvg(t1, "Avg. write: "); // es.shutdown(); es.shutdownNow(); es.awaitTermination(5, TimeUnit.SECONDS); }
/** * Creates a new {@link ThreadPoolExecutor} ready to carry out work. All pools are pre-started by * default and will terminate after not receiving work for the argued timeout value. * * @param poolName the name of this thread pool. * @param poolSize the size of this thread pool. * @param poolPriority the priority of this thread pool. * @param timeout how long in minutes it takes for threads in this pool to timeout. * @return the newly constructed thread pool. */ public static ThreadPoolExecutor createThreadPool( String poolName, int poolSize, int poolPriority, long timeout) { ThreadPoolExecutor threadPool = (ThreadPoolExecutor) Executors.newFixedThreadPool(poolSize); threadPool.setThreadFactory(new ThreadProvider(poolName, poolPriority, true)); threadPool.setRejectedExecutionHandler(new ThreadPoolRejectedExecutionHook()); threadPool.setKeepAliveTime(timeout, TimeUnit.MINUTES); threadPool.allowCoreThreadTimeOut(true); threadPool.prestartAllCoreThreads(); return threadPool; }
@Test public void testReadPerformance() throws Throwable { int size = 10; ExecutorService es = new ThreadPoolExecutor( size, Integer.MAX_VALUE, 30, TimeUnit.SECONDS, new SynchronousQueue()); ((ThreadPoolExecutor) es).prestartAllCoreThreads(); for (int i = 0; i < size; i++) es.execute(new WriteSpaceTask("PerformTask-" + i)); ISOUtil.sleep(500); printAvg(t1, "Avg. write: "); for (int i = 0; i < size; i++) es.execute(new ReadSpaceTask("PerformTask-" + i)); ISOUtil.sleep(500); printAvg(t2, "Avg. read : "); es.shutdown(); }
@Ignore("Remove it when TSpace can pass it") @Test public void testStolenEntryAtNotify() throws Throwable { int size = 10; final ExecutorService es = new ThreadPoolExecutor( size * 2, Integer.MAX_VALUE, 30, TimeUnit.SECONDS, new SynchronousQueue()); ((ThreadPoolExecutor) es).prestartAllCoreThreads(); for (int i = 0; i < size; i++) es.execute(new WriteSpaceWithNotifyReadTask("WriteTask-" + i)); // Threads which may stole entries for (int i = 0; i < size; i++) es.execute(new ReadSpaceTask("WriteTask-" + i)); assertNull("Detected stolen entry at notify", sp2.in("lost-entry", 200)); es.shutdownNow(); // es.awaitTermination(5, TimeUnit.SECONDS); }
/** * Constructor takes the following tunable configuration parameters * * @param numValues The number of values cached in the Queue for a particular key. * @param lowWatermark The ratio of (number of current entries/numValues) below which the <code> * fillQueueForKey()</code> funciton will be invoked to fill the Queue. * @param expiry Expiry time after which the Key and associated Queue are evicted from the cache. * @param numFillerThreads Number of threads to use for the filler thread * @param policy The SyncGenerationPolicy to use when client calls "getAtMost" * @param refiller implementation of the QueueRefiller */ public ValueQueue( final int numValues, final float lowWatermark, long expiry, int numFillerThreads, SyncGenerationPolicy policy, final QueueRefiller<E> refiller) { Preconditions.checkArgument(numValues > 0, "\"numValues\" must be > 0"); Preconditions.checkArgument( ((lowWatermark > 0) && (lowWatermark <= 1)), "\"lowWatermark\" must be > 0 and <= 1"); Preconditions.checkArgument(expiry > 0, "\"expiry\" must be > 0"); Preconditions.checkArgument(numFillerThreads > 0, "\"numFillerThreads\" must be > 0"); Preconditions.checkNotNull(policy, "\"policy\" must not be null"); this.refiller = refiller; this.policy = policy; this.numValues = numValues; this.lowWatermark = lowWatermark; keyQueues = CacheBuilder.newBuilder() .expireAfterAccess(expiry, TimeUnit.MILLISECONDS) .build( new CacheLoader<String, LinkedBlockingQueue<E>>() { @Override public LinkedBlockingQueue<E> load(String keyName) throws Exception { LinkedBlockingQueue<E> keyQueue = new LinkedBlockingQueue<E>(); refiller.fillQueueForKey(keyName, keyQueue, (int) (lowWatermark * numValues)); return keyQueue; } }); executor = new ThreadPoolExecutor( numFillerThreads, numFillerThreads, 0L, TimeUnit.MILLISECONDS, queue, new ThreadFactoryBuilder().setDaemon(true).setNameFormat(REFILL_THREAD).build()); // To ensure all requests are first queued, make coreThreads = maxThreads // and pre-start all the Core Threads. executor.prestartAllCoreThreads(); }
static void oneRun(BlockingQueue<Runnable> q, int nThreads, int iters, boolean print) throws Exception { ThreadPoolExecutor pool = new ThreadPoolExecutor(nThreads + 1, Integer.MAX_VALUE, 1L, TimeUnit.SECONDS, q); CountDownLatch done = new CountDownLatch(iters); remaining.set(nThreads - 1); pool.prestartAllCoreThreads(); Task t = new Task(pool, done); long start = System.nanoTime(); pool.execute(t); done.await(); long time = System.nanoTime() - start; if (print) System.out.println("\t: " + LoopHelpers.rightJustify(time / iters) + " ns per task"); q.clear(); Thread.sleep(100); pool.shutdown(); Thread.sleep(100); pool.shutdownNow(); }
@Override public void activateService() throws Exception { // Handle configuration defaults SchedulerConfiguration configuration = config.get(); Integer workersCount = configuration.workersCount().get(); Integer workQueueSize = configuration.workQueueSize().get(); if (workersCount == null) { workersCount = DEFAULT_WORKERS_COUNT; LOGGER.debug( "Workers count absent from configuration, falled back to default: {} workers", DEFAULT_WORKERS_COUNT); } if (workQueueSize == null) { workQueueSize = DEFAULT_WORKQUEUE_SIZE; LOGGER.debug( "WorkQueue size absent from configuration, falled back to default: {}", DEFAULT_WORKQUEUE_SIZE); } int corePoolSize = 2; if (workersCount > 4) { corePoolSize = workersCount / 4; } // Throws IllegalArgument if corePoolSize or keepAliveTime less than zero, or if workersCount // less than or equal to zero, or if corePoolSize greater than workersCount. taskExecutor = new ThreadPoolExecutor( corePoolSize, workersCount, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(workQueueSize), threadFactory, rejectionHandler); taskExecutor.prestartAllCoreThreads(); managementExecutor = new ScheduledThreadPoolExecutor(2, threadFactory, rejectionHandler); loadSchedules(); LOGGER.debug("Activated"); }
/** * Creates a RPC server with a thread-pool used to process requests. * * @param threads number of threads in a thread-pool. * @param queueSize thread-pool request queue size. */ public RPCServer(int threads, int queueSize) { if (threads < 0) throw new IllegalArgumentException("threads < 0"); if (threads > 0 && queueSize < 1) throw new IllegalArgumentException("queueSize < 1"); if (threads > 0) { threadPoll = new ThreadPoolExecutor( threads, threads, 0, TimeUnit.SECONDS, new ArrayBlockingQueue<Runnable>(queueSize)); threadPoll.prestartAllCoreThreads(); } else threadPoll = null; // sync processing channelProviderImpl = new RPCChannelProvider(threadPoll); serverContext = new ServerContextImpl(); serverContext.setBeaconServerStatusProvider(new DefaultBeaconServerDataProvider(serverContext)); try { serverContext.initialize(channelProviderImpl); } catch (Throwable th) { throw new RuntimeException("Failed to initialize pvAccess RPC server.", th); } }
public void testRehash() throws IOException, InterruptedException { EmbeddedCacheManager cacheManager = buildCacheManager(); cacheManagers.addLast(cacheManager); cacheManager.getCache("serviceGroup"); new AddNodeTask().run(); new AddNodeTask().run(); new AddNodeTask().run(); Thread.sleep(3000); log.info("Start testing"); ThreadPoolExecutor executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(TEST_THREADS); executor.prestartAllCoreThreads(); for (int i = 0; i < TEST_LOOPS; i++) { executor.submit(new SimulateTask()); } for (int i = 0; i < 10; i++) { try { Thread.sleep(3000); if (i != 1) { new AddNodeTask().run(); // 2 } else { new RemoveNodeTask().run(); } } catch (RuntimeException e) { log.warn("Error during add/remove node", e); } } log.info("Rehash phase is completed..."); executor.shutdown(); executor.awaitTermination(1, TimeUnit.DAYS); }
public synchronized void start() { // register ESP/CEP engine cepProvider = CEPProvider.getCEPProvider(); cepProvider.init(sleepListenerMillis); // register statements String suffix = Server.MODES.getProperty("_SUFFIX"); if ("NOOP".equals(mode)) {; } else { String stmtString = Server.MODES.getProperty(mode) + " " + suffix; System.out.println("Using " + mode + " : " + stmtString); if (Server.MODES.getProperty(mode).indexOf('$') < 0) { cepProvider.registerStatement(stmtString, mode); System.out.println("\nStatements registered # 1 only"); } else { // create a stmt for each symbol for (int i = 0; i < Symbols.SYMBOLS.length; i++) { if (i % 100 == 0) System.out.print("."); String ticker = Symbols.SYMBOLS[i]; cepProvider.registerStatement(stmtString.replaceAll("\\$", ticker), mode + "-" + ticker); } System.out.println("\nStatements registered # " + Symbols.SYMBOLS.length); } } // start thread pool if any if (queueMax < 0) { executor = null; System.out.println("Using direct handoff, cpu#" + Runtime.getRuntime().availableProcessors()); } else { // executor System.out.println( "Using ThreadPoolExecutor, cpu#" + Runtime.getRuntime().availableProcessors() + ", threadCore#" + threadCore + " queue#" + queueMax); BlockingQueue<Runnable> queue; if (queueMax == 0) { queue = new SynchronousQueue<Runnable>(true); // enforce fairness } else { queue = new LinkedBlockingQueue<Runnable>(queueMax); } executor = new ThreadPoolExecutor( threadCore, Runtime.getRuntime().availableProcessors() * threadCore, 10, TimeUnit.SECONDS, queue, new ThreadFactory() { long count = 0; public Thread newThread(Runnable r) { System.out.println("Create EsperServer thread " + (count + 1)); return new Thread(r, "EsperServer-" + count++); } }, new ThreadPoolExecutor.CallerRunsPolicy() { public void rejectedExecution(Runnable r, ThreadPoolExecutor e) { super.rejectedExecution(r, e); } }); executor.prestartAllCoreThreads(); } super.start(); }
/** * @param args * @throws JSONException * @throws ExecutionException * @throws InterruptedException */ public static void main(String[] args) throws JSONException, InterruptedException, ExecutionException { int numProcess = Integer.parseInt(args[0]); int numThread = Integer.parseInt(args[1]); boolean blocking = Boolean.parseBoolean(args[2]); if (numProcess <= 0) { System.out.println("Number of clients must be larger than 0."); System.exit(0); } final ThreadPoolExecutor executor; executor = new ThreadPoolExecutor( numProcess * numThread, numProcess * numThread, 0, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>()); executor.prestartAllCoreThreads(); String guid = "4B48F507395639FD806459281C3C09BCBB16FDFF"; String field = "someField"; String noop_code = ""; try { noop_code = new String(Files.readAllBytes(Paths.get("./scripts/activeCode/noop.js"))); } catch (IOException e) { e.printStackTrace(); } ValuesMap value = new ValuesMap(); value.put(field, "someValue"); // initialize a handler ActiveHandler handler = new ActiveHandler("", null, numProcess, numThread, blocking); ArrayList<Future<JSONObject>> tasks = new ArrayList<Future<JSONObject>>(); int n = 1000000; long t1 = System.currentTimeMillis(); for (int i = 0; i < n; i++) { tasks.add( executor.submit( new ActiveTask(clientPool[i % numProcess], guid, field, noop_code, value, 0))); } for (Future<JSONObject> task : tasks) { task.get(); } long elapsed = System.currentTimeMillis() - t1; System.out.println( "It takes " + elapsed + "ms, and the average latency for each operation is " + (elapsed * 1000.0 / n) + "us"); System.out.println("The throughput is " + Util.df(n * 1000.0 / elapsed) + "reqs/sec"); handler.shutdown(); System.out.println(DelayProfiler.getStats()); System.exit(0); }
@Before public void setUp() throws Exception { pool = new ThreadPoolExecutor(50, 50, 0, TimeUnit.SECONDS, Queues.newLinkedBlockingDeque()); pool.prestartAllCoreThreads(); counter = new CyclicCounter(10); }
public FixedThreadPoolWrapper(int threadCount, ThreadFactory factory) { this.executor = (ThreadPoolExecutor) Executors.newFixedThreadPool(threadCount, factory); executor.prestartAllCoreThreads(); }
private ThreadPool() { // private constructor GENERIC_THREAD_POOL.prestartAllCoreThreads(); }
@Override public void run() { threadsPool.prestartAllCoreThreads(); }