List of usage examples for java.util.concurrent Executors newFixedThreadPool
public static ExecutorService newFixedThreadPool(int nThreads)
From source file:com.b2international.index.GroovyMemoryLeakTest.java
@Ignore @Test//from w ww.j a v a 2 s . c o m public void tryToGenerateMemoryLeak() throws Exception { final List<String> orderedItems = newArrayList(); final Map<String, Data> documents = newHashMap(); for (int i = 0; i < NUM_DOCS; i++) { String item = null; while (item == null || orderedItems.contains(item)) { item = RandomStringUtils.randomAlphabetic(10); } orderedItems.add(item); final Data data = new Data(); data.setField1(item); data.setFloatField(100.0f - i); documents.put(Integer.toString(i), data); } indexDocuments(documents); ExecutorService executor = Executors.newFixedThreadPool(2); final Runnable theQuery = () -> { for (int i = 0; i < 10_000; i++) { final Query<Data> query = Query.select(Data.class) .where(Expressions.scriptScore(Expressions.matchAll(), "floatField")).limit(NUM_DOCS) .sortBy(SortBy.SCORE).build(); search(query); } }; // run 4 threads to simulate a bit higher load on the index executor.submit(theQuery, null); executor.submit(theQuery, null); executor.submit(theQuery, null); executor.submit(theQuery, null); executor.shutdown(); // this won't pass at all, even if the fix is applied // the purpose of this test to detect and verify the GC via external monitoring thus it cannot be automated properly assertTrue(executor.awaitTermination(5, TimeUnit.MINUTES)); }
From source file:org.exoplatform.utils.image.SocialImageLoader.java
public SocialImageLoader(Context context) { username = AccountSetting.getInstance().getUsername(); password = AccountSetting.getInstance().getPassword(); fileCache = new FileCache(context, ExoConstants.SOCIAL_FILE_CACHE); executorService = Executors.newFixedThreadPool(5); }
From source file:de.appsolve.padelcampus.utils.HtmlResourceUtil.java
public void updateCss(final ServletContext context) throws Exception { List<Customer> customers = customerDAO.findAll(); if (customers.isEmpty()) { applyCustomerCss(context, getDefaultCssAttributes(), ""); } else {/*from w w w. ja v a 2s. co m*/ lessCompiler = new LessCompiler(); lessCompiler.init(); int availableProcessors = Runtime.getRuntime().availableProcessors(); LOG.info(String.format("Compiling lesscss with %s cores", availableProcessors)); ExecutorService executor = Executors.newFixedThreadPool(availableProcessors); List<FutureTask<Void>> taskList = new ArrayList<>(); for (final Customer customer : customers) { FutureTask<Void> futureTask = new FutureTask<>(new Callable<Void>() { @Override public Void call() throws Exception { try { updateCss(context, customer); } catch (Exception ex) { LOG.error(ex, ex); } return null; } }); taskList.add(futureTask); executor.execute(futureTask); } for (FutureTask task : taskList) { task.get(); } executor.shutdown(); } }
From source file:com.barchart.udt.TestSocketFile.java
/** * verify basic file send/receive/*from w ww.ja va 2s . co m*/ */ @Test(timeout = 10 * 1000) public void fileTransfer() throws Exception { final InetSocketAddress addr1 = localSocketAddress(); final InetSocketAddress addr2 = localSocketAddress(); final SocketUDT peer1 = new SocketUDT(TypeUDT.STREAM); final SocketUDT peer2 = new SocketUDT(TypeUDT.STREAM); peer1.setBlocking(false); peer2.setBlocking(false); peer1.setRendezvous(true); peer2.setRendezvous(true); peer1.bind(addr1); peer2.bind(addr2); socketAwait(peer1, StatusUDT.OPENED); socketAwait(peer2, StatusUDT.OPENED); peer1.connect(addr2); peer2.connect(addr1); socketAwait(peer1, StatusUDT.CONNECTED); socketAwait(peer2, StatusUDT.CONNECTED); log.info("state 0 - connected"); log.info("peer1 : {}", peer1); log.info("peer2 : {}", peer2); final int size = 64 * 1024; final Random random = new Random(0); final byte[] array1 = new byte[size]; final byte[] array2 = new byte[size]; random.nextBytes(array1); random.nextBytes(array2); final File folder = new File("./target/file"); folder.mkdirs(); final File source = File.createTempFile("source", "data", folder); final File target = File.createTempFile("target", "data", folder); FileUtils.writeByteArrayToFile(source, array1); FileUtils.writeByteArrayToFile(target, array2); assertEquals(size, source.length()); assertEquals(size, target.length()); assertFalse("files are different", FileUtils.contentEquals(source, target)); // sender final Runnable task1 = new Runnable() { @Override public void run() { try { log.info("init send"); final long length = peer1.sendFile(source, 0, size); assertEquals(length, size); } catch (final Exception e) { log.error("", e); } } }; // receiver final Runnable task2 = new Runnable() { @Override public void run() { try { log.info("init recv"); final long length = peer2.receiveFile(target, 0, size); assertEquals(length, size); } catch (final Exception e) { log.error("", e); } } }; final ExecutorService executor = Executors.newFixedThreadPool(2); executor.submit(task1); executor.submit(task2); Thread.sleep(5 * 1000); executor.shutdownNow(); assertTrue("files are the same", FileUtils.contentEquals(source, target)); peer1.close(); peer2.close(); }
From source file:learn.jersey.services.BufferedMutatorExample.java
@Override public int run(String[] args) throws InterruptedException, ExecutionException, TimeoutException { /** a callback invoked when an asynchronous write fails. */ final BufferedMutator.ExceptionListener listener = new BufferedMutator.ExceptionListener() { @Override/*w w w. j a v a 2 s . co m*/ public void onException(RetriesExhaustedWithDetailsException e, BufferedMutator mutator) { for (int i = 0; i < e.getNumExceptions(); i++) { LOG.info("Failed to sent put " + e.getRow(i) + "."); } } }; BufferedMutatorParams params = new BufferedMutatorParams(TABLE).listener(listener); // // step 1: create a single Connection and a BufferedMutator, shared by // all worker threads. // try (final Connection conn = ConnectionFactory.createConnection(getConf()); final BufferedMutator mutator = conn.getBufferedMutator(params)) { /** worker pool that operates on BufferedTable instances */ final ExecutorService workerPool = Executors.newFixedThreadPool(POOL_SIZE); List<Future<Void>> futures = new ArrayList<>(TASK_COUNT); for (int i = 0; i < TASK_COUNT; i++) { futures.add(workerPool.submit(new Callable<Void>() { @Override public Void call() throws Exception { // // step 2: each worker sends edits to the shared // BufferedMutator instance. They all use // the same backing buffer, call-back "listener", and // RPC executor pool. // Put p = new Put(Bytes.toBytes("someRow")); p.addColumn(FAMILY, Bytes.toBytes("someQualifier"), Bytes.toBytes("some value")); mutator.mutate(p); // do work... maybe you want to call mutator.flush() // after many edits to ensure any of // this worker's edits are sent before exiting the // Callable return null; } })); } // // step 3: clean up the worker pool, shut down. // for (Future<Void> f : futures) { f.get(5, TimeUnit.MINUTES); } workerPool.shutdown(); } catch (IOException e) { // exception while creating/destroying Connection or BufferedMutator LOG.info("exception while creating/destroying Connection or BufferedMutator", e); } // BufferedMutator.close() ensures all work is flushed. Could be the // custom listener is // invoked from here. return 0; }
From source file:ai.grakn.engine.backgroundtasks.standalone.StandaloneTaskManager.java
private StandaloneTaskManager() { instantiatedTasks = new ConcurrentHashMap<>(); stateStorage = InMemoryStateStorage.getInstance(); stateUpdateLock = new ReentrantLock(); ConfigProperties properties = ConfigProperties.getInstance(); schedulingService = Executors.newScheduledThreadPool(1); executorService = Executors.newFixedThreadPool(properties.getAvailableThreads()); }
From source file:logdruid.util.DataMiner.java
public static MineResultSet gatherMineResultSet(final Repository repo) { String test = Preferences.getPreference("ThreadPool_Group"); int ini = Integer.parseInt(test); logger.info("gatherMineResultSet parallelism: " + ini); ThreadPool_GroupWorkers = Executors.newFixedThreadPool(ini); ChartData cd = new ChartData(); Collection<Callable<MineResult>> tasks = new ArrayList<Callable<MineResult>>(); MineResultSet mineResultSet = new MineResultSet(); cd = gatherSourceData(repo);//from w w w .ja v a 2s . c o m // if (logger.isEnabledFor(Level.INFO)) // logger.info("ArrayList sourceFileGroup" + sourceFileGroup); Iterator<Source> sourceIterator2 = repo.getSources().iterator(); while (sourceIterator2.hasNext()) { final Source source = sourceIterator2.next(); // sourceFiles contains all the matched files for a given source if (source.getActive()) { Iterator<Entry<String, ArrayList<FileRecord>>> it = cd.getGroupFilesMap(source).entrySet() .iterator(); while (it.hasNext()) { final Map.Entry<String, ArrayList<FileRecord>> pairs = (Map.Entry<String, ArrayList<FileRecord>>) it .next(); logger.debug("Source:" + source.getSourceName() + ", group: " + pairs.getKey() + " = " + pairs.getValue().toString()); tasks.add(new Callable<MineResult>() { public MineResult call() throws Exception { return DataMiner.mine((String) pairs.getKey(), (ArrayList<FileRecord>) pairs.getValue(), repo, source, Preferences.isStats(), Preferences.isTimings()); } }); } } } /* * invokeAll blocks until all service requests complete, or a max of * 1000 seconds. */ List<Future<MineResult>> results = null; try { results = ThreadPool_GroupWorkers.invokeAll(tasks, 1000, TimeUnit.SECONDS); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } for (Future<MineResult> f : results) { MineResult mineRes = null; try { // if (mineRes!=null) mineRes = f.get(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ExecutionException e) { // TODO Auto-generated catch block e.printStackTrace(); } if (mineRes != null) { mineResultSet.updateStartDate(mineRes.getStartDate()); mineResultSet.updateEndDate(mineRes.getEndDate()); if (!mineResultSet.mineResults.keySet().contains(mineRes.getSource())) { mineResultSet.mineResults.put(mineRes.getSource(), new HashMap<String, MineResult>()); } mineResultSet.mineResults.get(mineRes.getSource()) .put(mineRes.getSource().getSourceName() + mineRes.getGroup(), mineRes); } } return mineResultSet; }
From source file:br.com.mobclip.Application.java
public void submitTasks(int threads, Runnable runnable) { ExecutorService executor = Executors.newFixedThreadPool(threads); for (int i = 0; i < threads; i++) { executor.submit(runnable);//w ww .j av a2s.c o m } }
From source file:org.codice.pubsub.server.SubscriptionServer.java
public SubscriptionServer(BundleContext bundleContext, CatalogFramework catalogFramework, QueryAndSend queryAndSend) {//from w w w . j a v a2 s.co m this.bundleContext = bundleContext; this.catalogFramework = catalogFramework; this.queryAndSend = queryAndSend; processMap = new HashMap<String, Future<QueryControlInfo>>(); executor = Executors.newFixedThreadPool(NUM_QUERY_SEND_THREADS); }
From source file:com.netsteadfast.greenstep.bsc.command.KpiDateRangeScoreCommand.java
@Override public boolean execute(Context context) throws Exception { if (this.getResult(context) == null || !(this.getResult(context) instanceof BscStructTreeObj)) { return false; }/* w w w .j a va 2s. co m*/ BscStructTreeObj treeObj = (BscStructTreeObj) this.getResult(context); String frequency = (String) context.get("frequency"); String startYearDate = StringUtils.defaultString((String) context.get("startYearDate")).trim(); String endYearDate = StringUtils.defaultString((String) context.get("endYearDate")).trim(); String startDate = StringUtils.defaultString((String) context.get("startDate")).trim(); String endDate = StringUtils.defaultString((String) context.get("endDate")).trim(); //BscReportSupportUtils.loadExpression(); 2015-04-11 rem //long beg = System.currentTimeMillis(); for (VisionVO vision : treeObj.getVisions()) { for (PerspectiveVO perspective : vision.getPerspectives()) { for (ObjectiveVO objective : perspective.getObjectives()) { // 2015-04-11 add ExecutorService kpiCalculationPool = Executors .newFixedThreadPool(SimpleUtils.getAvailableProcessors(objective.getKpis().size())); for (KpiVO kpi : objective.getKpis()) { /* 2015-04-11 rem if (BscMeasureDataFrequency.FREQUENCY_DAY.equals(frequency) || BscMeasureDataFrequency.FREQUENCY_WEEK.equals(frequency) || BscMeasureDataFrequency.FREQUENCY_MONTH.equals(frequency) ) { this.fillDateRangeMonth(kpi, frequency, startDate, endDate); } else { this.fillDateRangeYear(kpi, frequency, startYearDate, endYearDate); } */ // 2015-04-11 add ScoreCalculationCallableData data = new ScoreCalculationCallableData(); data.setDefaultMode(false); data.setKpi(kpi); data.setFrequency(frequency); data.setDate1(startYearDate); data.setDate2(endYearDate); if (BscMeasureDataFrequency.FREQUENCY_DAY.equals(frequency) || BscMeasureDataFrequency.FREQUENCY_WEEK.equals(frequency) || BscMeasureDataFrequency.FREQUENCY_MONTH.equals(frequency)) { data.setDate1(startDate); data.setDate2(endDate); } data = kpiCalculationPool.submit(new ScoreCalculationCallable(data)).get(); } kpiCalculationPool.shutdown(); } } } //long end = System.currentTimeMillis(); //System.out.println( this.getClass().getName() + " use time(MS) = " + (end-beg) ); return false; }