List of usage examples for java.util.concurrent Executors newCachedThreadPool
public static ExecutorService newCachedThreadPool()
From source file:com.netflix.curator.framework.recipes.locks.TestInterProcessMutexBase.java
@Test public void testReentrantSingleLock() throws Exception { final int THREAD_QTY = 10; CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); client.start();/*from w w w. ja v a 2 s .co m*/ try { final AtomicBoolean hasLock = new AtomicBoolean(false); final AtomicBoolean isFirst = new AtomicBoolean(true); final Semaphore semaphore = new Semaphore(1); final InterProcessLock mutex = makeLock(client); List<Future<Object>> threads = Lists.newArrayList(); ExecutorService service = Executors.newCachedThreadPool(); for (int i = 0; i < THREAD_QTY; ++i) { Future<Object> t = service.submit(new Callable<Object>() { @Override public Object call() throws Exception { semaphore.acquire(); mutex.acquire(); Assert.assertTrue(hasLock.compareAndSet(false, true)); try { if (isFirst.compareAndSet(true, false)) { semaphore.release(THREAD_QTY - 1); while (semaphore.availablePermits() > 0) { Thread.sleep(100); } } else { Thread.sleep(100); } } finally { mutex.release(); hasLock.set(false); } return null; } }); threads.add(t); } for (Future<Object> t : threads) { t.get(); } } finally { client.close(); } }
From source file:com.microsoft.azure.AzureVMCloud.java
public synchronized static ExecutorService getThreadPool() { if (AzureVMCloud.threadPool == null) { AzureVMCloud.threadPool = Executors.newCachedThreadPool(); }//from ww w . j a va 2 s . co m return AzureVMCloud.threadPool; }
From source file:org.commonjava.indy.httprox.HttpProxyTest.java
@Before public void setup() throws Exception { contentMetadata.clear();/*from ww w .java 2 s. c o m*/ core.initGalley(); final TransportManager transports = new TransportManagerImpl( new HttpClientTransport(new HttpImpl(new MemoryPasswordManager()))); core.withTransportManager(transports); core.initMissingComponents(); final HttproxConfig config = new HttproxConfig(); config.setEnabled(true); proxyPort = config.getPort(); final BootOptions bootOpts = new BootOptions(); bootOpts.setBind(HOST); storeManager = new MemoryStoreDataManager(true); final IndyObjectMapper mapper = new IndyObjectMapper(true); final DefaultIndyConfiguration indyConfig = new DefaultIndyConfiguration(); indyConfig.setNotFoundCacheTimeoutSeconds(1); final ExpiringMemoryNotFoundCache nfc = new ExpiringMemoryNotFoundCache(indyConfig); WeftExecutorService rescanService = new PoolWeftExecutorService("test-rescan-executor", (ThreadPoolExecutor) Executors.newCachedThreadPool(), 2, 10f, false, null, null); final DownloadManager downloadManager = new DefaultDownloadManager(storeManager, core.getTransferManager(), core.getLocationExpander(), new MockInstance<>(new MockContentAdvisor()), nfc, rescanService); WeftExecutorService contentAccessService = new PoolWeftExecutorService("test-content-access-executor", (ThreadPoolExecutor) Executors.newCachedThreadPool(), 2, 10f, false, null, null); DirectContentAccess dca = new DefaultDirectContentAccess(downloadManager, contentAccessService); ContentDigester contentDigester = new DefaultContentDigester(dca, new CacheHandle<>("content-metadata", contentMetadata)); final ContentManager contentManager = new DefaultContentManager(storeManager, downloadManager, mapper, new SpecialPathManagerImpl(), new MemoryNotFoundCache(), contentDigester, Collections.emptySet()); DataFileManager dfm = new DataFileManager(temp.newFolder(), new DataFileEventManager()); final TemplatingEngine templates = new TemplatingEngine(new GStringTemplateEngine(), dfm); final ContentController contentController = new ContentController(storeManager, contentManager, templates, mapper, new MimeTyper()); KeycloakConfig kcConfig = new KeycloakConfig(); kcConfig.setEnabled(false); final KeycloakProxyAuthenticator auth = new KeycloakProxyAuthenticator(kcConfig, config); ScriptEngine scriptEngine = new ScriptEngine(dfm); proxy = new HttpProxy(config, bootOpts, new ProxyAcceptHandler(config, storeManager, contentController, auth, core.getCache(), scriptEngine, new MDCManager(), null, null, new CacheProducer(null, cacheManager, null))); proxy.start(); }
From source file:jp.aegif.nemaki.cmis.service.impl.AclServiceImpl.java
@Override public Acl applyAcl(CallContext callContext, String repositoryId, String objectId, Acl acl, AclPropagation aclPropagation) { exceptionService.invalidArgumentRequired("objectId", objectId); Lock lock = threadLockService.getReadLock(repositoryId, objectId); try {//from ww w . j ava2 s .c o m lock.lock(); // ////////////////// // General Exception // ////////////////// Content content = contentService.getContent(repositoryId, objectId); exceptionService.objectNotFound(DomainType.OBJECT, content, objectId); exceptionService.permissionDenied(callContext, repositoryId, PermissionMapping.CAN_APPLY_ACL_OBJECT, content); // ////////////////// // Specific Exception // ////////////////// TypeDefinition td = typeManager.getTypeDefinition(repositoryId, content); if (!td.isControllableAcl()) exceptionService.constraint(objectId, "applyAcl cannot be performed on the object whose controllableAcl = false"); exceptionService.constraintAclPropagationDoesNotMatch(aclPropagation); exceptionService.constraintPermissionDefined(repositoryId, acl, objectId); // ////////////////// // Body of the method // ////////////////// //Check ACL inheritance boolean inherited = true; //Inheritance defaults to true if nothing input List<CmisExtensionElement> exts = acl.getExtensions(); if (!CollectionUtils.isEmpty(exts)) { for (CmisExtensionElement ext : exts) { if (ext.getName().equals("inherited")) { inherited = Boolean.valueOf(ext.getValue()); } } if (!contentService.getAclInheritedWithDefault(repositoryId, content).equals(inherited)) content.setAclInherited(inherited); } jp.aegif.nemaki.model.Acl nemakiAcl = new jp.aegif.nemaki.model.Acl(); //REPOSITORYDETERMINED or PROPAGATE is considered as PROPAGATE boolean objectOnly = (aclPropagation == AclPropagation.OBJECTONLY) ? true : false; for (Ace ace : acl.getAces()) { if (ace.isDirect()) { jp.aegif.nemaki.model.Ace nemakiAce = new jp.aegif.nemaki.model.Ace(ace.getPrincipalId(), ace.getPermissions(), objectOnly); nemakiAcl.getLocalAces().add(nemakiAce); } } convertSystemPrinciaplId(repositoryId, nemakiAcl); content.setAcl(nemakiAcl); contentService.update(repositoryId, content); contentService.writeChangeEvent(callContext, repositoryId, content, nemakiAcl, ChangeType.SECURITY); nemakiCachePool.get(repositoryId).removeCmisCache(objectId); clearCachesRecursively(Executors.newCachedThreadPool(), callContext, repositoryId, content, false); writeChangeEventsRecursively(Executors.newCachedThreadPool(), callContext, repositoryId, content, false); return getAcl(callContext, repositoryId, objectId, false); } finally { lock.unlock(); } }
From source file:com.ottogroup.bi.spqr.pipeline.component.queue.chronicle.DefaultStreamingMessageQueueTest.java
/** * Inserts a configurable number of messages into a {@link Chronicle} and measures the * duration it takes to read the content from it using the {@link DefaultStreamingMessageQueue} implementation *///from w w w.j a v a2 s. c o m // @Test public void testNext_performanceTest() throws Exception { Properties props = new Properties(); props.put(DefaultStreamingMessageQueue.CFG_CHRONICLE_QUEUE_DELETE_ON_EXIT, "true"); props.put(DefaultStreamingMessageQueue.CFG_CHRONICLE_QUEUE_PATH, System.getProperty("java.io.tmpdir")); final DefaultStreamingMessageQueue inbox = new DefaultStreamingMessageQueue(); inbox.setId("testNext_performanceTest"); inbox.initialize(props); final StreamingMessageQueueProducer producer = inbox.getProducer(); final StreamingMessageQueueConsumer consumer = inbox.getConsumer(); final CountDownLatch latch = new CountDownLatch(numberOfMessagesPerfTest); ExecutorService svc = Executors.newCachedThreadPool(); Future<Integer> producerDurationFuture = svc.submit(new Callable<Integer>() { public Integer call() { StreamingDataMessage object = new StreamingDataMessage(new byte[] { 01, 2, 3, 4, 5, 6, 7, 9 }, System.currentTimeMillis()); long s1 = System.nanoTime(); for (int i = 0; i < numberOfMessagesPerfTest; i++) { producer.insert(object); } long s2 = System.nanoTime(); return (int) (s2 - s1); } }); Future<Integer> durationFuture = svc.submit(new Callable<Integer>() { public Integer call() { StreamingDataMessage msg = null; long start = System.nanoTime(); while (true) { msg = consumer.next(); if (msg != null) { latch.countDown(); if (latch.getCount() == 0) break; } else { LockSupport.parkNanos(1); } } long end = System.nanoTime(); return (int) (end - start); } }); try { Assert.assertTrue("Failed to receive expected number of messages", latch.await(10, TimeUnit.SECONDS)); } catch (InterruptedException e) { Assert.fail("Failed to receive expected number of messages"); } int producerDuration = producerDurationFuture.get(); int duration = durationFuture.get(); double messagesPerNano = ((double) numberOfMessagesPerfTest / (double) duration); double messagesPerNanoRounded = (double) Math.round(messagesPerNano * 10000) / 10000; double messagesPerMilli = messagesPerNano * 1000000; messagesPerMilli = (double) Math.round(messagesPerMilli * 100) / 100; long messagesPerSecondTmps = Math.round(messagesPerNano * 1000000 * 1000); double messagesPerSecond = (double) Math.round(messagesPerSecondTmps); ; double nanosPerMessage = ((double) duration / (double) numberOfMessagesPerfTest); nanosPerMessage = (double) Math.round(nanosPerMessage * 100) / 100; logger.info("message count: " + numberOfMessagesPerfTest); logger.info( "message producing: " + producerDuration + "ns, " + TimeUnit.NANOSECONDS.toMillis(producerDuration) + "ms, " + TimeUnit.NANOSECONDS.toSeconds(producerDuration) + "s"); logger.info("message consumption: " + duration + "ns, " + TimeUnit.NANOSECONDS.toMillis(duration) + "ms, " + TimeUnit.NANOSECONDS.toSeconds(duration) + "s"); logger.info("message throughput: " + messagesPerNanoRounded + " msgs/ns, " + messagesPerMilli + " msgs/ms, " + messagesPerSecond + " msgs/s"); svc.shutdownNow(); }
From source file:ch.aonyx.broker.ib.api.SocketSession.java
@SuppressWarnings("unchecked") private void createDisruptor() { disruptor = new Disruptor<EventWrapper>(new EventWrapperFactory(), RING_BUFFER_SIZE, Executors.newCachedThreadPool()); disruptor.handleEventsWith(new ConcurrentlyEventHandler(eventNotifier)); }
From source file:com.linkedin.pinot.transport.netty.NettySingleConnectionIntegrationTest.java
@Test public void testValidatePool() throws Exception { PooledNettyClientResourceManager resourceManager = new PooledNettyClientResourceManager( new NioEventLoopGroup(), new HashedWheelTimer(), new NettyClientMetrics(null, "abc")); ExecutorService executorService = Executors.newCachedThreadPool(); ScheduledExecutorService timeoutExecutor = new ScheduledThreadPoolExecutor(5); String serverName = "server"; MetricsRegistry metricsRegistry = new MetricsRegistry(); AsyncPoolResourceManagerAdapter<PooledNettyClientResourceManager.PooledClientConnection> rmAdapter = new AsyncPoolResourceManagerAdapter<>( _clientServer, resourceManager, executorService, metricsRegistry); AsyncPool<PooledNettyClientResourceManager.PooledClientConnection> pool = new AsyncPoolImpl<>(serverName, rmAdapter, /*maxSize=*/5, /*idleTimeoutMs=*/100000L, timeoutExecutor, executorService, /*maxWaiters=*/10, AsyncPoolImpl.Strategy.LRU, /*minSize=*/2, metricsRegistry); try {//w w w . ja v a 2 s . c o m pool.start(); Uninterruptibles.sleepUninterruptibly(1L, TimeUnit.SECONDS); // Test no connection in pool Assert.assertTrue(pool.validate(false)); PoolStats stats = pool.getStats(); Assert.assertEquals(stats.getPoolSize(), 2); Assert.assertEquals(stats.getTotalBadDestroyed(), 0); Assert.assertEquals(stats.getCheckedOut(), 0); // Test one connection, it should not destroy anything AsyncResponseFuture<PooledNettyClientResourceManager.PooledClientConnection> responseFuture = new AsyncResponseFuture<>( _clientServer, null); pool.get(responseFuture); Assert.assertNotNull(responseFuture.getOne()); Assert.assertTrue(pool.validate(false)); stats = pool.getStats(); Assert.assertEquals(stats.getPoolSize(), 2); Assert.assertEquals(stats.getTotalBadDestroyed(), 0); Assert.assertEquals(stats.getCheckedOut(), 1); // Now stop the server, so that the checked out connection is invalidated closeServerConnection(); Assert.assertTrue(pool.validate(false)); stats = pool.getStats(); Assert.assertEquals(stats.getPoolSize(), 2); Assert.assertEquals(stats.getTotalBadDestroyed(), 1); Assert.assertEquals(stats.getCheckedOut(), 1); } finally { pool.shutdown(new Callback<NoneType>() { @Override public void onSuccess(NoneType arg0) { } @Override public void onError(Throwable arg0) { Assert.fail("Shutdown error"); } }); executorService.shutdown(); timeoutExecutor.shutdown(); } }
From source file:oz.hadoop.yarn.api.core.LocalApplicationLaunchTests.java
@Test(timeout = 2000) public void validateJavaContainerLaunchImmediateTermination() throws Exception { final YarnApplication<Void> yarnApplication = YarnAssembly .forApplicationContainer(SimpleRandomDelayContainer.class, ByteBuffer.wrap("Hello".getBytes())) .containerCount(2).memory(512).withApplicationMaster().maxAttempts(2).priority(2) .build("sample-yarn-application"); assertFalse(yarnApplication.isRunning()); ExecutorService executor = Executors.newCachedThreadPool(); executor.execute(new Runnable() { @Override/*from w w w.j av a 2s.c o m*/ public void run() { yarnApplication.launch(); } }); assertFalse(yarnApplication.isRunning()); yarnApplication.terminate(); assertEquals(0, yarnApplication.liveContainers()); assertFalse(yarnApplication.isRunning()); executor.shutdown(); }
From source file:co.pugo.convert.ConvertServlet.java
/** * download imageData and encode it base64 * @param imageLinks set of image links extracted with extractImageLinks() * @return map, key = imageLink, value = base64 encoded image *///from ww w . j a v a 2s . c o m private HashMap<String, String> downloadImageData(Set<String> imageLinks) { HashMap<String, String> imageData = new HashMap<>(); ExecutorService service = Executors.newCachedThreadPool(); for (final String imageLink : imageLinks) { RunnableFuture<byte[]> future = new FutureTask<>(new Callable<byte[]>() { @Override public byte[] call() { try { URL srcUrl = new URL(imageLink); URLConnection urlConnection = srcUrl.openConnection(); return IOUtils.toByteArray(urlConnection.getInputStream()); } catch (IOException e) { LOG.severe(e.getMessage()); return null; } } }); service.execute(future); try { imageData.put(imageLink, Base64.encodeBase64String(future.get())); } catch (InterruptedException | ExecutionException e) { LOG.severe(e.getMessage()); } } service.shutdown(); try { service.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { LOG.severe(e.getMessage()); } return imageData; }
From source file:com.android.framework.core.http.AsyncHttpClient.java
/** * Creates a new AsyncHttpClient.// ww w . j av a2s .c om */ public AsyncHttpClient() { BasicHttpParams httpParams = new BasicHttpParams(); ConnManagerParams.setTimeout(httpParams, socketTimeout); ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(maxConnections)); ConnManagerParams.setMaxTotalConnections(httpParams, DEFAULT_MAX_CONNECTIONS); HttpConnectionParams.setSoTimeout(httpParams, socketTimeout); HttpConnectionParams.setConnectionTimeout(httpParams, socketTimeout); HttpConnectionParams.setTcpNoDelay(httpParams, true); HttpConnectionParams.setSocketBufferSize(httpParams, DEFAULT_SOCKET_BUFFER_SIZE); HttpProtocolParams.setVersion(httpParams, HttpVersion.HTTP_1_1); HttpProtocolParams.setUserAgent(httpParams, String.format("android client - http veirsion(%s)", VERSION)); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(httpParams, schemeRegistry); httpContext = new SyncBasicHttpContext(new BasicHttpContext()); httpClient = new DefaultHttpClient(cm, httpParams); httpClient.addRequestInterceptor(new HttpRequestInterceptor() { @Override public void process(HttpRequest request, HttpContext context) { if (!request.containsHeader(HEADER_ACCEPT_ENCODING)) { request.addHeader(HEADER_ACCEPT_ENCODING, ENCODING_GZIP); } for (String header : clientHeaderMap.keySet()) { request.addHeader(header, clientHeaderMap.get(header)); } } }); httpClient.addResponseInterceptor(new HttpResponseInterceptor() { @Override public void process(HttpResponse response, HttpContext context) { final HttpEntity entity = response.getEntity(); if (entity == null) { return; } final Header encoding = entity.getContentEncoding(); if (encoding != null) { for (HeaderElement element : encoding.getElements()) { if (element.getName().equalsIgnoreCase(ENCODING_GZIP)) { response.setEntity(new InflatingEntity(response.getEntity())); break; } } } } }); httpClient.setHttpRequestRetryHandler(new RetryHandler(DEFAULT_MAX_RETRIES)); threadPool = (ThreadPoolExecutor) Executors.newCachedThreadPool(); requestMap = new WeakHashMap<Context, List<WeakReference<Future<?>>>>(); clientHeaderMap = new HashMap<String, String>(); }