List of usage examples for java.util.concurrent Executors newFixedThreadPool
public static ExecutorService newFixedThreadPool(int nThreads)
From source file:cp.server.app.ClientMultiThreadedExecution.java
public static void fetch() throws Exception { // Create an HttpClient with the ThreadSafeClientConnManager. // This connection manager must be used if more than one thread will // be using the HttpClient. // PoolingHttpClientConnectionManager cm = new // PoolingHttpClientConnectionManager(); // cm.setMaxTotal(100); // CloseableHttpClient httpclient = HttpClients.custom() // .setConnectionManager(cm).build(); ExecutorService pool = Executors.newFixedThreadPool(10); ServerDAO dao = new ServerDAO(); List<Page> pages = null; Time ts = new Time(System.currentTimeMillis()); int interval; try {// www. ja v a2 s .c o m // // before 10am, query with the comment yesterday // if (Integer.valueOf(ts.toString().substring(0, 2)) > 10) // { // interval = 1; // } // else // { // interval = 2; // } // // pages = dao.queryPagesByDayInterval( // ConnectionFactory.getConnection(), interval); // // System.out.println("load comments from " + pages.size() + // "pages."); // for (Page page : pages) // { // PAGESTACK.push(page.getUrl()); // } } catch (Exception ex) { ex.printStackTrace(); } try { // create an array of URIs to perform GETs on String[] urisToGet = { "http://sports.sina.com.cn", "http://news.sina.com.cn", "http://ent.sina.com.cn", "http://tech.sina.com.cn", "http://sports.sina.com.cn/o/2013-10-27/04016852444.shtml", "http://finance.sina.com.cn/china/20131027/043917125695.shtml", "http://sports.sina.com.cn/j/2013-10-27/06336852561.shtml", "http://sports.sina.com.cn/j/2013-10-26/21006851844.shtml" }; for (int i = 0; i < 10000; i++) { for (int j = 0; j < urisToGet.length; j++) { PAGESTACK.push(urisToGet[j]); } } CountDownLatch cdl = new CountDownLatch(6); // create a thread for each URI GetThread[] threads = new GetThread[urisToGet.length]; for (int i = 0; i < 4; i++) { // HttpGet httpget = new HttpGet(urisToGet[i]); threads[i] = new GetThread(urisToGet[i], i + 1, cdl); } // start the threads for (int j = 0; j < 4; j++) { pool.execute(threads[j]); // threads[j].start(); } cdl.await(); } finally { // httpclient.close(); pool.shutdown(); } }
From source file:com.flipkart.bifrost.ListenTest.java
@Test public void testSendReceive() throws Exception { ObjectMapper mapper = new ObjectMapper(); mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL); mapper.setSerializationInclusion(JsonInclude.Include.NON_EMPTY); Connection connection = new Connection(Lists.newArrayList("localhost"), "guest", "guest"); connection.start();// w w w .j a v a 2 s. com BifrostExecutor<Void> executor = BifrostExecutor.<Void>builder(TestAction.class).connection(connection) .objectMapper(mapper).requestQueue("bifrost-send").responseQueue("bifrost-recv").concurrency(20) .executorService(Executors.newFixedThreadPool(20)).build(); BifrostRemoteCallExecutionServer<Void> executionServer = BifrostRemoteCallExecutionServer .<Void>builder(TestAction.class).objectMapper(mapper).connection(connection).concurrency(20) .requestQueue("bifrost-send").build(); executionServer.start(); long startTime = System.currentTimeMillis(); AtomicInteger counter = new AtomicInteger(0); int requestCount = 1000000; CompletionService<Void> ecs = new ExecutorCompletionService<>(Executors.newFixedThreadPool(50)); List<Future<Void>> futures = Lists.newArrayListWithCapacity(requestCount); for (int i = 0; i < requestCount; i++) { futures.add(ecs.submit(new ServiceCaller(executor, counter))); } for (int i = 0; i < requestCount; i++) { try { ecs.take().get(); } catch (ExecutionException e) { e.printStackTrace(); } } while (counter.get() != requestCount) ; System.out.println( String.format("Completed: %d in %d ms", counter.get(), (System.currentTimeMillis() - startTime))); executor.shutdown(); executionServer.stop(); connection.stop(); Assert.assertEquals(requestCount, counter.get()); }
From source file:com.qwazr.library.test.CassandraTest.java
@Test public void test_10_transaction() throws Exception { ExecutorService executor = Executors.newFixedThreadPool(10); try {/*from w w w .j av a2 s . co m*/ cassandra.execute("SELECT count(*) FROM qwazr_connector_test.test").all(); finalTime = System.currentTimeMillis() + 10000; List<ProcedureExceptionCatcher> threadList = new ArrayList<ProcedureExceptionCatcher>(); for (int i = 0; i < 50; i++) { threadList.add(new InsertThread()); threadList.add(new SelectUpdateThread()); } ThreadUtils.invokeAndJoin(executor, threadList); for (CallableExceptionCatcher<?> callable : threadList) callable.checkException(); } catch (NoHostAvailableException e) { logger.warning("Bypass (no cassandra host is running)"); } finally { executor.shutdown(); } }
From source file:edu.isi.wings.execution.engine.api.impl.local.LocalExecutionEngine.java
public LocalExecutionEngine(Properties props) { this.props = props; if (props.containsKey("parallel")) this.maxParallel = Integer.parseInt(props.getProperty("parallel")); this.stepEngine = this; this.planEngine = this; executor = Executors.newFixedThreadPool(maxParallel); }
From source file:io.cloudslang.content.utilities.util.ProcessExecutor.java
public ProcessResponseEntity execute(String commandLine, int timeout) throws IOException, ExecutionException, InterruptedException, TimeoutException { Process process = new ProcessBuilder().command(processCommand(commandLine)).start(); ProcessStreamConsumer processStreamConsumer = new ProcessStreamConsumer(process); ExecutorService executor = Executors.newFixedThreadPool(1); Future<ProcessResponseEntity> futureResult = executor.submit(processStreamConsumer); try {//from www. ja va 2 s. co m return futureResult.get(timeout, MILLISECONDS); } catch (InterruptedException | ExecutionException | TimeoutException e) { stopProcess(process, futureResult); throw e; } finally { executor.shutdownNow(); } }
From source file:com.openshift.restclient.server.HttpServerFake.java
public void start() throws Exception { executor = Executors.newFixedThreadPool(1); this.serverFakeSocket = createServerFakeSocket(statusLine, response, port); executor.submit(serverFakeSocket);/*w ww . java 2 s .c om*/ }
From source file:de.xaniox.heavyspleef.core.Updater.java
public Updater(Plugin plugin) { ExecutorService execService = Executors.newFixedThreadPool(THREAD_POOL_SIZE); this.service = MoreExecutors.listeningDecorator(execService); this.plugin = plugin; this.desc = plugin.getDescription(); this.updateFolder = plugin.getServer().getUpdateFolderFile(); Bukkit.getPluginManager().registerEvents(this, plugin); }
From source file:com.zxy.commons.mq.consumer.AbstractConsumer.java
/** * // w w w. j a v a 2 s .c o m * * @param groupId groupId * @param topic topic * @param threadNum thread number */ public AbstractConsumer(String groupId, String topic, Integer threadNum) { this.topic = topic; if (threadNum != null) { this.threadNum = threadNum; this.executor = Executors.newFixedThreadPool(threadNum); } else { this.threadNum = 1; this.executor = Executors.newSingleThreadExecutor(); } Properties props = new Properties(); props.put("zookeeper.connect", PROP.getZookeeperConnect()); props.put("group.id", groupId); props.put("zookeeper.session.timeout.ms", PROP.getZookeeperSessionTimeout()); props.put("zookeeper.sync.time.ms", PROP.getZookeeperSyncTime()); props.put("auto.commit.interval.ms", PROP.getAutoCommitInterval()); String autoCommitEnable = PROP.getAutoCommitEnable(); if (StringUtils.isNotBlank(autoCommitEnable)) { props.put("auto.commit.enable", autoCommitEnable); } consumer = kafka.consumer.Consumer.createJavaConsumerConnector(new ConsumerConfig(props)); }
From source file:org.yamj.core.service.ArtworkProcessScheduler.java
@Scheduled(initialDelay = 30000, fixedDelay = 60000) public void processArtwork() throws Exception { int maxThreads = configService.getIntProperty("yamj3.scheduler.artworkprocess.maxThreads", 1); if (maxThreads <= 0) { if (!messageDisabled) { messageDisabled = Boolean.TRUE; LOG.info("Artwork processing is disabled"); }/*from w w w.j av a 2s. c o m*/ return; } else { messageDisabled = Boolean.FALSE; } int maxResults = configService.getIntProperty("yamj3.scheduler.artworkprocess.maxResults", 20); List<QueueDTO> queueElements = artworkStorageService.getArtworLocatedQueue(maxResults); if (CollectionUtils.isEmpty(queueElements)) { LOG.debug("No artwork found to process"); return; } LOG.info("Found {} artwork objects to process; process with {} threads", queueElements.size(), maxThreads); BlockingQueue<QueueDTO> queue = new LinkedBlockingQueue<QueueDTO>(queueElements); ExecutorService executor = Executors.newFixedThreadPool(maxThreads); for (int i = 0; i < maxThreads; i++) { ArtworkProcessRunner worker = new ArtworkProcessRunner(queue, artworkProcessorService); executor.execute(worker); } executor.shutdown(); // run until all workers have finished while (!executor.isTerminated()) { try { TimeUnit.SECONDS.sleep(5); } catch (InterruptedException ignore) { } } LOG.debug("Finished artwork processing"); }