List of usage examples for java.util.concurrent Executors newCachedThreadPool
public static ExecutorService newCachedThreadPool()
From source file:com.netflix.curator.framework.recipes.locks.TestInterProcessSemaphore.java
@Test public void testReleaseInChunks() throws Exception { final int MAX_LEASES = 11; final int THREADS = 100; final CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); client.start();//from w ww . j a va 2 s .c o m try { final Stepper latch = new Stepper(); final Random random = new Random(); final Counter counter = new Counter(); ExecutorService service = Executors.newCachedThreadPool(); ExecutorCompletionService<Object> completionService = new ExecutorCompletionService<Object>(service); for (int i = 0; i < THREADS; ++i) { completionService.submit(new Callable<Object>() { @Override public Object call() throws Exception { InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, "/test", MAX_LEASES); Lease lease = semaphore.acquire(10, TimeUnit.SECONDS); if (lease == null) { throw new Exception("timed out"); } try { synchronized (counter) { ++counter.currentCount; if (counter.currentCount > counter.maxCount) { counter.maxCount = counter.currentCount; } counter.notifyAll(); } latch.await(); } finally { synchronized (counter) { --counter.currentCount; } semaphore.returnLease(lease); } return null; } }); } int remaining = THREADS; while (remaining > 0) { int times = Math.min(random.nextInt(5) + 1, remaining); latch.countDown(times); remaining -= times; Thread.sleep(random.nextInt(100) + 1); } for (int i = 0; i < THREADS; ++i) { completionService.take(); } synchronized (counter) { Assert.assertTrue(counter.currentCount == 0); Assert.assertTrue(counter.maxCount > 0); Assert.assertTrue(counter.maxCount <= MAX_LEASES); System.out.println(counter.maxCount); } } finally { client.close(); } }
From source file:co.paralleluniverse.galaxy.netty.UDPComm.java
@Override public void init() throws Exception { super.init(); if (!isSendToServerInsteadOfMulticast() && multicastGroup == null) { LOG.error("If sendToServerInsteadOfBroadcast, multicastGroup must be set!"); throw new RuntimeException("multicastGroup not set."); }// ww w .ja v a 2 s . co m this.myAddress = new InetSocketAddress(InetAddress.getLocalHost(), port); if (workerExecutor == null) workerExecutor = (ThreadPoolExecutor) Executors.newCachedThreadPool(); // Netty ignores executor thread naming strategy cause of Worker renaming policy. // org.jboss.netty.channel.socket.nio.AbstractNioWorker.newThreadRenamingRunnable() // And unfortunately for NioDatagramChannelFactory itsn't possible to pass our own ThreadNameDeterminer. configureThreadPool(getWorkerExecutorName(), workerExecutor); if (receiveExecutor != null) configureThreadPool(getReceiveExecutorName(), receiveExecutor); this.channelFactory = isSendToServerInsteadOfMulticast() ? new NioDatagramChannelFactory(workerExecutor, NettyUtils.getWorkerCount(workerExecutor)) : new OioDatagramChannelFactory(workerExecutor); this.bootstrap = new ConnectionlessBootstrap(channelFactory); this.bootstrap.setOption("receiveBufferSizePredictorFactory", new FixedReceiveBufferSizePredictorFactory(4096)); bootstrap.setPipelineFactory(new UdpMessagePipelineFactory(LOG, new ChannelNodeAddressResolver(addressResolver), receiveExecutor) { @Override public ChannelPipeline getPipeline() throws Exception { final ChannelPipeline pipeline = super.getPipeline(); pipeline.addLast("router", new SimpleChannelHandler() { @Override public void messageReceived(ChannelHandlerContext ctx, MessageEvent e) { if (ctx.getChannel() == multicastChannel) { if (e.getRemoteAddress().equals(myAddress)) return; // this is our own multicast ((MessagePacket) e.getMessage()).setMulticast(); } UDPComm.this.messageReceived((MessagePacket) e.getMessage()); } @Override public void exceptionCaught(ChannelHandlerContext ctx, ExceptionEvent e) { LOG.info("Channel exception: {} {}", e.getCause().getClass().getName(), e.getCause().getMessage()); LOG.debug("Channel exception", e.getCause()); } }); return pipeline; } }); bootstrap.setOption("localAddress", new InetSocketAddress(InetAddress.getLocalHost(), port)); bootstrap.setOption("tcpNoDelay", true); monitor.registerMBean(); }
From source file:com.all.messengine.impl.ResponseManager.java
public void initialize() { listeners = Collections.synchronizedMap(new HashMap<String, ResponseLock>()); executor = Executors.newCachedThreadPool(); }
From source file:com.codefollower.lealone.omid.client.TSOClient.java
public TSOClient(Configuration conf) throws IOException { state = State.DISCONNECTED;//w ww . jav a 2 s .co m queuedOps = new ArrayBlockingQueue<Op>(200); retryTimer = new Timer(true); commitCallbacks = Collections.synchronizedMap(new HashMap<Long, CommitCallback>()); isCommittedCallbacks = Collections.synchronizedMap(new HashMap<Long, List<CommitQueryCallback>>()); createCallbacks = new ConcurrentLinkedQueue<CreateCallback>(); channel = null; LOG.info("Starting TSOClient"); // Start client with Nb of active threads = 3 as maximum. factory = new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool(), 3); // Create the bootstrap bootstrap = new ClientBootstrap(factory); int executorThreads = conf.getInt("tso.executor.threads", 3); bootstrap.getPipeline().addLast("executor", new ExecutionHandler( new OrderedMemoryAwareThreadPoolExecutor(executorThreads, 1024 * 1024, 4 * 1024 * 1024))); bootstrap.getPipeline().addLast("handler", this); bootstrap.setOption("tcpNoDelay", false); bootstrap.setOption("keepAlive", true); bootstrap.setOption("reuseAddress", true); bootstrap.setOption("connectTimeoutMillis", 100); String host = conf.get("tso.host"); int port = conf.getInt("tso.port", 1234); maxRetries = conf.getInt("tso.max_retries", 100); retryDelayMs = conf.getInt("tso.retry_delay_ms", 1000); if (host == null) { throw new IOException("tso.host missing from configuration"); } addr = new InetSocketAddress(host, port); connectIfNeeded(); }
From source file:oz.hadoop.yarn.api.core.LocalApplicationLaunchTests.java
@Test(timeout = 2000) public void validateContainerLaunchWithInfiniteCommandForcedShutdown() throws Exception { ClassPathResource resource = new ClassPathResource("infinite", this.getClass()); final YarnApplication<Void> yarnApplication = YarnAssembly .forApplicationContainer(resource.getFile().getAbsolutePath()).containerCount(3).memory(512) .withApplicationMaster().maxAttempts(2).priority(2).build("sample-yarn-application"); assertEquals(0, yarnApplication.liveContainers()); assertFalse(yarnApplication.isRunning()); ExecutorService executor = Executors.newCachedThreadPool(); executor.execute(new Runnable() { @Override/* w ww. ja v a 2 s. c o m*/ public void run() { yarnApplication.launch(); } }); while (yarnApplication.liveContainers() != 3) { LockSupport.parkNanos(1000); } assertTrue(yarnApplication.isRunning()); yarnApplication.terminate(); assertEquals(0, yarnApplication.liveContainers()); }
From source file:com.netflix.curator.framework.recipes.locks.TestInterProcessMutexBase.java
@Test public void test2Clients() throws Exception { CuratorFramework client1 = null;/*w w w . j av a2s . c o m*/ CuratorFramework client2 = null; try { client1 = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); client2 = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); client1.start(); client2.start(); final InterProcessLock mutexForClient1 = makeLock(client1); final InterProcessLock mutexForClient2 = makeLock(client2); final CountDownLatch latchForClient1 = new CountDownLatch(1); final CountDownLatch latchForClient2 = new CountDownLatch(1); final CountDownLatch acquiredLatchForClient1 = new CountDownLatch(1); final CountDownLatch acquiredLatchForClient2 = new CountDownLatch(1); final AtomicReference<Exception> exceptionRef = new AtomicReference<Exception>(); ExecutorService service = Executors.newCachedThreadPool(); Future<Object> future1 = service.submit(new Callable<Object>() { @Override public Object call() throws Exception { try { mutexForClient1.acquire(10, TimeUnit.SECONDS); acquiredLatchForClient1.countDown(); latchForClient1.await(10, TimeUnit.SECONDS); mutexForClient1.release(); } catch (Exception e) { exceptionRef.set(e); } return null; } }); Future<Object> future2 = service.submit(new Callable<Object>() { @Override public Object call() throws Exception { try { mutexForClient2.acquire(10, TimeUnit.SECONDS); acquiredLatchForClient2.countDown(); latchForClient2.await(10, TimeUnit.SECONDS); mutexForClient2.release(); } catch (Exception e) { exceptionRef.set(e); } return null; } }); while (!mutexForClient1.isAcquiredInThisProcess() && !mutexForClient2.isAcquiredInThisProcess()) { Thread.sleep(1000); Assert.assertFalse(future1.isDone() && future2.isDone()); } Assert.assertTrue( mutexForClient1.isAcquiredInThisProcess() != mutexForClient2.isAcquiredInThisProcess()); Thread.sleep(1000); Assert.assertTrue( mutexForClient1.isAcquiredInThisProcess() || mutexForClient2.isAcquiredInThisProcess()); Assert.assertTrue( mutexForClient1.isAcquiredInThisProcess() != mutexForClient2.isAcquiredInThisProcess()); Exception exception = exceptionRef.get(); if (exception != null) { throw exception; } if (mutexForClient1.isAcquiredInThisProcess()) { latchForClient1.countDown(); Assert.assertTrue(acquiredLatchForClient2.await(10, TimeUnit.SECONDS)); Assert.assertTrue(mutexForClient2.isAcquiredInThisProcess()); } else { latchForClient2.countDown(); Assert.assertTrue(acquiredLatchForClient1.await(10, TimeUnit.SECONDS)); Assert.assertTrue(mutexForClient1.isAcquiredInThisProcess()); } } finally { IOUtils.closeQuietly(client1); IOUtils.closeQuietly(client2); } }
From source file:com.all.messengine.impl.DefaultMessEngine.java
public DefaultMessEngine() { queue = new LinkedBlockingQueue<Message<?>>(); messageExecutor = Executors.newCachedThreadPool(); engineExecutor = Executors.newSingleThreadExecutor(); listeners = new HashMap<String, List<MessageListener<? extends Message<?>>>>(); responseManager = new ResponseManager(); shuttingDown = new AtomicBoolean(false); }
From source file:com.github.tteofili.looseen.Test20NewsgroupsClassification.java
@Test public void test20Newsgroups() throws Exception { String indexProperty = System.getProperty("index"); if (indexProperty != null) { try {//from w w w. ja v a 2 s. c o m index = Boolean.valueOf(indexProperty); } catch (Exception e) { // ignore } } String splitProperty = System.getProperty("split"); if (splitProperty != null) { try { split = Boolean.valueOf(splitProperty); } catch (Exception e) { // ignore } } Path mainIndexPath = Paths.get(INDEX + "/original"); Directory directory = FSDirectory.open(mainIndexPath); Path trainPath = Paths.get(INDEX + "/train"); Path testPath = Paths.get(INDEX + "/test"); Path cvPath = Paths.get(INDEX + "/cv"); FSDirectory cv = null; FSDirectory test = null; FSDirectory train = null; IndexReader testReader = null; if (split) { cv = FSDirectory.open(cvPath); test = FSDirectory.open(testPath); train = FSDirectory.open(trainPath); } if (index) { delete(mainIndexPath); if (split) { delete(trainPath, testPath, cvPath); } } IndexReader reader = null; List<Classifier<BytesRef>> classifiers = new LinkedList<>(); try { Analyzer analyzer = new StandardAnalyzer(); if (index) { System.out.format("Indexing 20 Newsgroups...%n"); long startIndex = System.currentTimeMillis(); IndexWriter indexWriter = new IndexWriter(directory, new IndexWriterConfig(analyzer)); buildIndex(new File(PREFIX + "/20n/20_newsgroups"), indexWriter); long endIndex = System.currentTimeMillis(); System.out.format("Indexed %d pages in %ds %n", indexWriter.maxDoc(), (endIndex - startIndex) / 1000); indexWriter.close(); } if (split && !index) { reader = DirectoryReader.open(train); } else { reader = DirectoryReader.open(directory); } if (index && split) { // split the index System.out.format("Splitting the index...%n"); long startSplit = System.currentTimeMillis(); DatasetSplitter datasetSplitter = new DatasetSplitter(0.1, 0); datasetSplitter.split(reader, train, test, cv, analyzer, false, CATEGORY_FIELD, BODY_FIELD, SUBJECT_FIELD, CATEGORY_FIELD); reader.close(); reader = DirectoryReader.open(train); // using the train index from now on long endSplit = System.currentTimeMillis(); System.out.format("Splitting done in %ds %n", (endSplit - startSplit) / 1000); } final long startTime = System.currentTimeMillis(); classifiers.add(new KNearestNeighborClassifier(reader, new ClassicSimilarity(), analyzer, null, 1, 0, 0, CATEGORY_FIELD, BODY_FIELD)); classifiers.add(new KNearestNeighborClassifier(reader, null, analyzer, null, 1, 0, 0, CATEGORY_FIELD, BODY_FIELD)); classifiers.add(new KNearestNeighborClassifier(reader, new ClassicSimilarity(), analyzer, null, 3, 0, 0, CATEGORY_FIELD, BODY_FIELD)); classifiers.add(new KNearestNeighborClassifier(reader, new AxiomaticF1EXP(), analyzer, null, 3, 0, 0, CATEGORY_FIELD, BODY_FIELD)); classifiers.add(new KNearestNeighborClassifier(reader, new AxiomaticF1LOG(), analyzer, null, 3, 0, 0, CATEGORY_FIELD, BODY_FIELD)); classifiers.add(new KNearestNeighborClassifier(reader, new LMDirichletSimilarity(), analyzer, null, 3, 1, 1, CATEGORY_FIELD, BODY_FIELD)); classifiers.add(new KNearestNeighborClassifier(reader, new LMJelinekMercerSimilarity(0.3f), analyzer, null, 3, 1, 1, CATEGORY_FIELD, BODY_FIELD)); classifiers.add(new KNearestNeighborClassifier(reader, null, analyzer, null, 3, 1, 1, CATEGORY_FIELD, BODY_FIELD)); classifiers.add(new KNearestNeighborClassifier(reader, new DFRSimilarity(new BasicModelG(), new AfterEffectB(), new NormalizationH1()), analyzer, null, 3, 1, 1, CATEGORY_FIELD, BODY_FIELD)); classifiers.add(new KNearestNeighborClassifier(reader, new DFRSimilarity(new BasicModelP(), new AfterEffectL(), new NormalizationH3()), analyzer, null, 3, 1, 1, CATEGORY_FIELD, BODY_FIELD)); classifiers.add(new KNearestNeighborClassifier(reader, new IBSimilarity(new DistributionSPL(), new LambdaDF(), new Normalization.NoNormalization()), analyzer, null, 3, 1, 1, CATEGORY_FIELD, BODY_FIELD)); classifiers.add(new KNearestNeighborClassifier(reader, new IBSimilarity(new DistributionLL(), new LambdaTTF(), new NormalizationH1()), analyzer, null, 3, 1, 1, CATEGORY_FIELD, BODY_FIELD)); classifiers.add(new MinHashClassifier(reader, BODY_FIELD, CATEGORY_FIELD, 15, 1, 100)); classifiers.add(new MinHashClassifier(reader, BODY_FIELD, CATEGORY_FIELD, 30, 3, 300)); classifiers.add(new MinHashClassifier(reader, BODY_FIELD, CATEGORY_FIELD, 10, 1, 100)); classifiers.add(new KNearestFuzzyClassifier(reader, new LMJelinekMercerSimilarity(0.3f), analyzer, null, 1, CATEGORY_FIELD, BODY_FIELD)); classifiers.add(new KNearestFuzzyClassifier(reader, new IBSimilarity(new DistributionLL(), new LambdaTTF(), new NormalizationH1()), analyzer, null, 1, CATEGORY_FIELD, BODY_FIELD)); classifiers.add(new KNearestFuzzyClassifier(reader, new ClassicSimilarity(), analyzer, null, 1, CATEGORY_FIELD, BODY_FIELD)); classifiers.add(new KNearestFuzzyClassifier(reader, new ClassicSimilarity(), analyzer, null, 3, CATEGORY_FIELD, BODY_FIELD)); classifiers .add(new KNearestFuzzyClassifier(reader, null, analyzer, null, 1, CATEGORY_FIELD, BODY_FIELD)); classifiers .add(new KNearestFuzzyClassifier(reader, null, analyzer, null, 3, CATEGORY_FIELD, BODY_FIELD)); classifiers.add(new KNearestFuzzyClassifier(reader, new AxiomaticF1EXP(), analyzer, null, 3, CATEGORY_FIELD, BODY_FIELD)); classifiers.add(new KNearestFuzzyClassifier(reader, new AxiomaticF1LOG(), analyzer, null, 3, CATEGORY_FIELD, BODY_FIELD)); classifiers.add(new BM25NBClassifier(reader, analyzer, null, CATEGORY_FIELD, BODY_FIELD)); classifiers.add(new CachingNaiveBayesClassifier(reader, analyzer, null, CATEGORY_FIELD, BODY_FIELD)); classifiers.add(new SimpleNaiveBayesClassifier(reader, analyzer, null, CATEGORY_FIELD, BODY_FIELD)); int maxdoc; if (split) { testReader = DirectoryReader.open(test); maxdoc = testReader.maxDoc(); } else { maxdoc = reader.maxDoc(); } System.out.format("Starting evaluation on %d docs...%n", maxdoc); ExecutorService service = Executors.newCachedThreadPool(); List<Future<String>> futures = new LinkedList<>(); for (Classifier<BytesRef> classifier : classifiers) { testClassifier(reader, startTime, testReader, service, futures, classifier); } for (Future<String> f : futures) { System.out.println(f.get()); } Thread.sleep(10000); service.shutdown(); } finally { if (reader != null) { reader.close(); } directory.close(); if (test != null) { test.close(); } if (train != null) { train.close(); } if (cv != null) { cv.close(); } if (testReader != null) { testReader.close(); } for (Classifier c : classifiers) { if (c instanceof Closeable) { ((Closeable) c).close(); } } } }
From source file:com.alliander.osgp.webdevicesimulator.application.config.ApplicationContext.java
@Bean(destroyMethod = "releaseExternalResources") public ClientBootstrap clientBootstrap() { final ChannelFactory factory = new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()); final ChannelPipelineFactory pipelineFactory = new ChannelPipelineFactory() { @Override//from w w w . j a va2 s . c om public ChannelPipeline getPipeline() throws InvalidKeySpecException, NoSuchAlgorithmException, IOException, NoSuchProviderException { final ChannelPipeline pipeline = ApplicationContext.this.createPipeLine(); LOGGER.info("Created new client pipeline"); return pipeline; } }; final ClientBootstrap bootstrap = new ClientBootstrap(factory); bootstrap.setOption("tcpNoDelay", true); bootstrap.setOption("keepAlive", false); bootstrap.setOption("connectTimeoutMillis", this.connectionTimeout()); bootstrap.setPipelineFactory(pipelineFactory); return bootstrap; }
From source file:com.microsoft.azure.servicebus.samples.deadletterqueue.DeadletterQueue.java
private void waitForEnter(int seconds) { ExecutorService executor = Executors.newCachedThreadPool(); try {// w w w .j a v a 2s .c o m executor.invokeAny(Arrays.asList(() -> { System.in.read(); return 0; }, () -> { Thread.sleep(seconds * 1000); return 0; })); } catch (Exception e) { // absorb } }