List of usage examples for java.util.concurrent Executors newFixedThreadPool
public static ExecutorService newFixedThreadPool(int nThreads)
From source file:ok.MyService2.java
@Override protected Task<BlockingQueue> createTask() { final Task<BlockingQueue> task; task = new Task<BlockingQueue>() { @Override//from w w w . j av a 2 s .c om protected BlockingQueue call() throws Exception { BlockingQueue result = new LinkedBlockingQueue<String>(); PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(); cm.setMaxTotal(100); CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build(); try { ExecutorService executor = Executors.newFixedThreadPool(sites.size()); List<Future<String>> results = new ArrayList<Future<String>>(); for (int i = 0; i < sites.size(); i++) { HttpGet httpget = new HttpGet(sites.get(i)); Callable worker = new MyCallable(httpclient, httpget); Future<String> res = executor.submit(worker); results.add(res); // String url = hostList[i]; // Runnable worker = new MyRunnable(url); // executor.execute(worker); // executor.submit(null); } executor.shutdown(); // Wait until all threads are finish // while (!executor.isTerminated()) { // // } for (Future<String> element : results) { result.add(element.get()); } System.out.println("\nFinished all threads"); } finally { httpclient.close(); } return result; } }; return task; }
From source file:com.serphacker.serposcope.task.proxy.ProxyChecker.java
@Override public void run() { LOG.info("starting proxy checking task, threads = {}, timeout in MS = {}", nThread, timeoutMS); long start = System.currentTimeMillis(); List<Proxy> proxies = db.proxy.list(); if (proxies == null || proxies.isEmpty()) { LOG.debug("no proxy to check"); return;// ww w.jav a2 s .co m } totalProxies = proxies.size(); ExecutorService executor = Executors.newFixedThreadPool(nThread); db.proxy.updateStatus(Proxy.Status.UNCHECKED, proxies.stream().map((t) -> t.getId()).collect(Collectors.toList())); for (Proxy proxy : proxies) { executor.submit(new Runnable() { @Override public void run() { ScrapClient cli = new ScrapClient(); cli.setTimeout(timeoutMS); ScrapProxy scrapProxy = proxy.toScrapProxy(); cli.setProxy(scrapProxy); LOG.info("checking {}", scrapProxy); Proxy.Status proxyStatus = Proxy.Status.ERROR; // try{Thread.sleep(30000l);}catch(Exception ex){} int httpStatus = cli.get(judgeUrl); if (httpStatus == 200 && cli.getContentAsString() != null) { Matcher matcher = PATTERN_IP.matcher(cli.getContentAsString()); if (matcher.find()) { proxy.setRemoteip(matcher.group(1)); proxyStatus = Proxy.Status.OK; } } proxy.setStatus(proxyStatus); proxy.setLastCheck(LocalDateTime.now()); db.proxy.update(proxy); checked.incrementAndGet(); } }); } executor.shutdown(); try { executor.awaitTermination(1, TimeUnit.HOURS); } catch (InterruptedException ex) { executor.shutdownNow(); } LOG.info("proxy checking finished in {}", DurationFormatUtils.formatDurationHMS(System.currentTimeMillis() - start)); }
From source file:com.newlandframework.avatarmq.core.SendMessageCache.java
public void parallelDispatch(LinkedList<MessageDispatchTask> list) { List<Callable<Void>> tasks = new ArrayList<Callable<Void>>(); int startPosition = 0; Pair<Integer, Integer> pair = calculateBlocks(list.size(), list.size()); int numberOfThreads = pair.getRight(); int blocks = pair.getLeft(); for (int i = 0; i < numberOfThreads; i++) { MessageDispatchTask[] task = new MessageDispatchTask[blocks]; phaser.register();//from w ww .j a v a 2s .com System.arraycopy(list.toArray(), startPosition, task, 0, blocks); tasks.add(new SendMessageTask(phaser, task)); startPosition += blocks; } ExecutorService executor = Executors.newFixedThreadPool(numberOfThreads); for (Callable<Void> element : tasks) { executor.submit(element); } }
From source file:com.subgraph.vega.internal.crawler.WebCrawler.java
WebCrawler(IHttpRequestEngine requestEngine, int requestThreadCount, int responseThreadCount) { this.requestEngine = requestEngine; this.requestThreadCount = requestThreadCount; this.responseThreadCount = responseThreadCount; this.executor = Executors.newFixedThreadPool(requestThreadCount + responseThreadCount); this.requestConsumers = new ArrayList<RequestConsumer>(requestThreadCount); this.responseProcessors = new ArrayList<HttpResponseProcessor>(responseThreadCount); this.eventHandlers = new ArrayList<ICrawlerProgressTracker>(); }
From source file:io.undertow.server.handlers.file.FileHandlerStressTestCase.java
@Test public void simpleFileStressTest() throws IOException, ExecutionException, InterruptedException, URISyntaxException { ExecutorService executor = Executors.newFixedThreadPool(NUM_THREADS); try {//from www . jav a2 s .c o m Path rootPath = Paths.get(getClass().getResource("page.html").toURI()).getParent(); final ResourceHandler handler = new ResourceHandler(new PathResourceManager(rootPath, 10485760)); final CacheHandler cacheHandler = new CacheHandler(new DirectBufferCache(1024, 10, 10480), handler); final PathHandler path = new PathHandler(); path.addPrefixPath("/path", cacheHandler); final CanonicalPathHandler root = new CanonicalPathHandler(); root.setNext(path); DefaultServer.setRootHandler(root); final List<Future<?>> futures = new ArrayList<>(); for (int i = 0; i < NUM_THREADS; ++i) { futures.add(executor.submit(new Runnable() { @Override public void run() { TestHttpClient client = new TestHttpClient(); try { for (int i = 0; i < NUM_REQUESTS; ++i) { HttpGet get = new HttpGet(DefaultServer.getDefaultServerURL() + "/path/page.html"); HttpResponse result = client.execute(get); Assert.assertEquals(StatusCodes.OK, result.getStatusLine().getStatusCode()); final String response = HttpClientUtils.readResponse(result); Assert.assertTrue(response, response.contains("A web page")); } } catch (IOException e) { throw new RuntimeException(e); } finally { client.getConnectionManager().shutdown(); } } })); } for (Future<?> future : futures) { future.get(); } } finally { executor.shutdown(); } }
From source file:com.netflix.curator.framework.recipes.locks.TestInterProcessSemaphoreCluster.java
@Test public void testKilledServerWithEnsembleProvider() throws Exception { final int CLIENT_QTY = 10; final Timing timing = new Timing(); final String PATH = "/foo/bar/lock"; ExecutorService executorService = Executors.newFixedThreadPool(CLIENT_QTY); ExecutorCompletionService<Void> completionService = new ExecutorCompletionService<Void>(executorService); TestingCluster cluster = new TestingCluster(3); try {/* w w w . j a va 2s .c o m*/ cluster.start(); final AtomicReference<String> connectionString = new AtomicReference<String>( cluster.getConnectString()); final EnsembleProvider provider = new EnsembleProvider() { @Override public void start() throws Exception { } @Override public String getConnectionString() { return connectionString.get(); } @Override public void close() throws IOException { } }; final Semaphore acquiredSemaphore = new Semaphore(0); final AtomicInteger acquireCount = new AtomicInteger(0); final CountDownLatch suspendedLatch = new CountDownLatch(CLIENT_QTY); for (int i = 0; i < CLIENT_QTY; ++i) { completionService.submit(new Callable<Void>() { @Override public Void call() throws Exception { CuratorFramework client = CuratorFrameworkFactory.builder().ensembleProvider(provider) .sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()) .retryPolicy(new ExponentialBackoffRetry(100, 3)).build(); try { final Semaphore suspendedSemaphore = new Semaphore(0); client.getConnectionStateListenable().addListener(new ConnectionStateListener() { @Override public void stateChanged(CuratorFramework client, ConnectionState newState) { if ((newState == ConnectionState.SUSPENDED) || (newState == ConnectionState.LOST)) { suspendedLatch.countDown(); suspendedSemaphore.release(); } } }); client.start(); InterProcessSemaphoreV2 semaphore = new InterProcessSemaphoreV2(client, PATH, 1); while (!Thread.currentThread().isInterrupted()) { Lease lease = null; try { lease = semaphore.acquire(); acquiredSemaphore.release(); acquireCount.incrementAndGet(); suspendedSemaphore.acquire(); } catch (Exception e) { // just retry } finally { if (lease != null) { acquireCount.decrementAndGet(); IOUtils.closeQuietly(lease); } } } } finally { IOUtils.closeQuietly(client); } return null; } }); } Assert.assertTrue(timing.acquireSemaphore(acquiredSemaphore)); Assert.assertEquals(1, acquireCount.get()); cluster.close(); timing.awaitLatch(suspendedLatch); timing.forWaiting().sleepABit(); Assert.assertEquals(0, acquireCount.get()); cluster = new TestingCluster(3); cluster.start(); connectionString.set(cluster.getConnectString()); timing.forWaiting().sleepABit(); Assert.assertTrue(timing.acquireSemaphore(acquiredSemaphore)); timing.forWaiting().sleepABit(); Assert.assertEquals(1, acquireCount.get()); } finally { executorService.shutdown(); executorService.awaitTermination(10, TimeUnit.SECONDS); executorService.shutdownNow(); IOUtils.closeQuietly(cluster); } }
From source file:org.gitana.platform.load.AbstractLoadTest.java
protected ExecutorService createExecutorService() { int corePoolSize = getNumberOfRunners(); //int maximumPoolSize = getNumberOfRunners(); //int keepAliveTime = 1; //TimeUnit timeUnit = TimeUnit.MINUTES; //BlockingQueue<Runnable> workQueue = new ArrayBlockingQueue<Runnable>(maximumPoolSize, true); //RejectedExecutionHandler rejectedExecutionHandler = new ThreadPoolExecutor.CallerRunsPolicy(); //int nrOfProcessors = Runtime.getRuntime().availableProcessors(); return Executors.newFixedThreadPool(corePoolSize); //return new ThreadPoolExecutor(corePoolSize, maximumPoolSize, keepAliveTime, timeUnit, workQueue, rejectedExecutionHandler); }
From source file:net.anthavio.vinbudin.VinbudinBoot.java
@Bean public net.anthavio.vaadin.CallbackRegistry CallbackRegistry() { ExecutorService executor = Executors.newFixedThreadPool(10); return new net.anthavio.vaadin.CallbackRegistry(executor, 3, TimeUnit.SECONDS); }
From source file:net.arp7.HdfsPerfTest.WriteFile.java
private static void writeFiles(final Configuration conf, final FileIoStats stats) throws InterruptedException, IOException { final FileSystem fs = FileSystem.get(conf); final AtomicLong filesLeft = new AtomicLong(params.getNumFiles()); final long runId = abs(rand.nextLong()); final byte[] data = new byte[params.getIoSize()]; Arrays.fill(data, (byte) 65); // Start the writers. final ExecutorService executor = Executors.newFixedThreadPool((int) params.getNumThreads()); final CompletionService<Object> ecs = new ExecutorCompletionService<>(executor); LOG.info("NumFiles=" + params.getNumFiles() + ", FileSize=" + FileUtils.byteCountToDisplaySize(params.getFileSize()) + ", IoSize=" + FileUtils.byteCountToDisplaySize(params.getIoSize()) + ", BlockSize=" + FileUtils.byteCountToDisplaySize(params.getBlockSize()) + ", ReplicationFactor=" + params.getReplication() + ", isThrottled=" + (params.maxWriteBps() > 0)); LOG.info("Starting " + params.getNumThreads() + " writer thread" + (params.getNumThreads() > 1 ? "s" : "") + "."); final long startTime = System.nanoTime(); for (long t = 0; t < params.getNumThreads(); ++t) { final long threadIndex = t; Callable<Object> c = new Callable<Object>() { @Override/*w w w. j av a2 s. c o m*/ public Object call() throws Exception { long fileIndex = 0; while (filesLeft.addAndGet(-1) >= 0) { final String fileName = "WriteFile-" + runId + "-" + (threadIndex + 1) + "-" + (++fileIndex); writeOneFile(new Path(params.getOutputDir(), fileName), fs, data, stats); } return null; } }; ecs.submit(c); } // And wait for all writers to complete. for (long t = 0; t < params.getNumThreads(); ++t) { ecs.take(); } final long endTime = System.nanoTime(); stats.setElapsedTime(endTime - startTime); executor.shutdown(); }
From source file:gsn.tests.performance.Queries.java
public Queries(int nbQueries, int nbThreads) { this.nbQueries = nbQueries; this.nbThreads = nbThreads; executor = Executors.newFixedThreadPool(nbThreads); saxParserFactory = SAXParserFactory.newInstance(); mapping = new HashMap<String, ArrayList<DataField>>(); }