List of usage examples for java.util.concurrent ExecutorService submit
Future<?> submit(Runnable task);
From source file:com.netflix.curator.framework.recipes.queue.TestDistributedQueue.java
@Test public void testMultiPutterSingleGetter() throws Exception { final int itemQty = 100; DistributedQueue<TestQueueItem> queue = null; CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); client.start();/* ww w . java 2 s. c om*/ try { BlockingQueueConsumer<TestQueueItem> consumer = new BlockingQueueConsumer<TestQueueItem>( Mockito.mock(ConnectionStateListener.class)); queue = QueueBuilder.builder(client, consumer, serializer, QUEUE_PATH).buildQueue(); queue.start(); QueueTestProducer producer1 = new QueueTestProducer(queue, itemQty / 2, 0); QueueTestProducer producer2 = new QueueTestProducer(queue, ((itemQty + 1) / 2), itemQty / 2); ExecutorService service = Executors.newCachedThreadPool(); service.submit(producer1); service.submit(producer2); int iteration = 0; while (consumer.size() < itemQty) { Assert.assertTrue(++iteration < 10); Thread.sleep(1000); } List<TestQueueItem> items = consumer.getItems(); Assert.assertEquals(com.google.common.collect.Sets.<TestQueueItem>newHashSet(items).size(), items.size()); // check no duplicates } finally { IOUtils.closeQuietly(queue); IOUtils.closeQuietly(client); } }
From source file:com.netflix.curator.framework.recipes.queue.TestDistributedQueue.java
@Test public void testSafetyBasic() throws Exception { final int itemQty = 10; DistributedQueue<TestQueueItem> queue = null; CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); client.start();//w w w . ja v a 2 s .c om try { final BlockingQueueConsumer<TestQueueItem> consumer = new BlockingQueueConsumer<TestQueueItem>( Mockito.mock(ConnectionStateListener.class)); queue = QueueBuilder.builder(client, consumer, serializer, QUEUE_PATH).lockPath("/a/locks") .buildQueue(); queue.start(); QueueTestProducer producer = new QueueTestProducer(queue, itemQty, 0); ExecutorService service = Executors.newCachedThreadPool(); service.submit(producer); final CountDownLatch latch = new CountDownLatch(1); service.submit(new Callable<Object>() { @Override public Object call() throws Exception { for (int i = 0; i < itemQty; ++i) { TestQueueItem item = consumer.take(); Assert.assertEquals(item.str, Integer.toString(i)); } latch.countDown(); return null; } }); Assert.assertTrue(latch.await(10, TimeUnit.SECONDS)); } finally { IOUtils.closeQuietly(queue); IOUtils.closeQuietly(client); } }
From source file:ch.jamiete.hilda.plugins.PluginManager.java
public void disablePlugins() { ExecutorService executor = Executors.newSingleThreadExecutor(); synchronized (this.plugins) { final Iterator<HildaPlugin> iterator = this.plugins.iterator(); while (iterator.hasNext()) { final HildaPlugin entry = iterator.next(); Future<?> future = executor.submit(() -> { try { entry.onDisable();/*from w ww . ja v a 2s .c om*/ } catch (final Exception e) { Hilda.getLogger().log(Level.WARNING, "Encountered an exception while disabling plugin " + entry.getPluginData().getName(), e); } }); try { future.get(30, TimeUnit.SECONDS); } catch (InterruptedException | ExecutionException | TimeoutException e) { Hilda.getLogger().log(Level.WARNING, "Plugin " + entry.getPluginData().getName() + " took too long disabling; ceased executing its code", e); } } } executor.shutdown(); try { executor.awaitTermination(5, TimeUnit.SECONDS); } catch (InterruptedException e) { Hilda.getLogger().log(Level.WARNING, "Encountered an exception during the plugin disable grace period", e); } }
From source file:com.netflix.curator.framework.recipes.atomic.TestDistributedAtomicLong.java
@Test public void testSimulation() throws Exception { final int threadQty = 20; final int executionQty = 50; final AtomicInteger optimisticTries = new AtomicInteger(); final AtomicInteger promotedLockTries = new AtomicInteger(); final AtomicInteger failures = new AtomicInteger(); final AtomicInteger errors = new AtomicInteger(); final SummaryStatistics timingStats = new SynchronizedSummaryStatistics(); List<Future<Void>> procs = Lists.newArrayList(); ExecutorService executorService = Executors.newFixedThreadPool(threadQty); for (int i = 0; i < threadQty; ++i) { Callable<Void> proc = new Callable<Void>() { @Override/* w w w. ja v a2 s . c o m*/ public Void call() throws Exception { doSimulation(executionQty, timingStats, optimisticTries, promotedLockTries, failures, errors); return null; } }; procs.add(executorService.submit(proc)); } for (Future<Void> f : procs) { f.get(); } System.out.println("OptimisticTries: " + optimisticTries.get()); System.out.println("PromotedLockTries: " + promotedLockTries.get()); System.out.println("Failures: " + failures.get()); System.out.println("Errors: " + errors.get()); System.out.println(); System.out.println("Avg time: " + timingStats.getMean()); System.out.println("Max time: " + timingStats.getMax()); System.out.println("Min time: " + timingStats.getMin()); System.out.println("Qty: " + timingStats.getN()); Assert.assertEquals(errors.get(), 0); Assert.assertTrue(optimisticTries.get() > 0); Assert.assertTrue(promotedLockTries.get() > 0); }
From source file:com.netflix.curator.framework.recipes.queue.TestDistributedQueue.java
@Test public void testPutListener() throws Exception { final int itemQty = 10; DistributedQueue<TestQueueItem> queue = null; CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), new RetryOneTime(1)); client.start();/*from ww w .j a v a 2 s .com*/ try { BlockingQueueConsumer<TestQueueItem> consumer = new BlockingQueueConsumer<TestQueueItem>( Mockito.mock(ConnectionStateListener.class)); queue = QueueBuilder.builder(client, consumer, serializer, QUEUE_PATH).buildQueue(); queue.start(); QueueTestProducer producer = new QueueTestProducer(queue, itemQty, 0); final AtomicInteger listenerCalls = new AtomicInteger(0); QueuePutListener<TestQueueItem> listener = new QueuePutListener<TestQueueItem>() { @Override public void putCompleted(TestQueueItem item) { listenerCalls.incrementAndGet(); } @Override public void putMultiCompleted(MultiItem<TestQueueItem> items) { } }; queue.getPutListenerContainer().addListener(listener); ExecutorService service = Executors.newCachedThreadPool(); service.submit(producer); int iteration = 0; while (consumer.size() < itemQty) { Assert.assertTrue(++iteration < 10); Thread.sleep(1000); } int i = 0; for (TestQueueItem item : consumer.getItems()) { Assert.assertEquals(item.str, Integer.toString(i++)); } Assert.assertEquals(listenerCalls.get(), itemQty); } finally { IOUtils.closeQuietly(queue); IOUtils.closeQuietly(client); } }
From source file:com.smartitengineering.dao.hbase.autoincrement.AutoIncrementRowIdForLongTest.java
@Test public void testMultithreadedKeys() throws Exception { ExecutorService service = Executors.newFixedThreadPool(THREAD_COUNT); final long start = 102; final int bound = THREAD_COUNT; final int innerBound = 30; List<Future> futures = new ArrayList<Future>(); final long startTime = System.currentTimeMillis(); for (int i = 0; i < bound; ++i) { final int index = i; futures.add(service.submit(new Runnable() { @Override// www.j av a2 s. co m public void run() { for (int j = 0; j < innerBound; ++j) { final HTableInterface table = pool.getTable(TABLE_NAME); long id = index * bound + j + start; final long id1 = getId(); synchronized (ids) { final long mainId = Long.MAX_VALUE - id1; Assert.assertFalse(ids.contains(mainId)); ids.add(mainId); } final byte[] row = Bytes.toBytes(id1); Put put = new Put(row); final byte[] toBytes = Bytes.toBytes("value " + id); put.add(FAMILY_BYTES, CELL_BYTES, toBytes); Result get1; try { table.put(put); Get get = new Get(row); get1 = table.get(get); } catch (Exception ex) { LOGGER.error(ex.getMessage(), ex); get1 = null; Assert.fail(ex.getMessage()); } pool.putTable(table); Assert.assertNotNull(get1); Assert.assertFalse(get1.isEmpty()); Assert.assertTrue(Arrays.equals(row, get1.getRow())); Assert.assertTrue(Arrays.equals(toBytes, get1.getValue(FAMILY_BYTES, CELL_BYTES))); } } })); } for (Future future : futures) { future.get(); } final long endTime = System.currentTimeMillis(); LOGGER.info("Time for " + (bound * innerBound) + " rows ID retrieval, put and get is " + (endTime - startTime) + "ms"); Assert.assertEquals(bound * innerBound + start - 1, ids.size()); }
From source file:com.alibaba.otter.node.etl.common.db.DbPerfIntergration.java
@Test public void test_stack() { DbMediaSource dbMediaSource = new DbMediaSource(); dbMediaSource.setId(1L);//from w w w . j a v a 2s.c om dbMediaSource.setDriver("com.mysql.jdbc.Driver"); dbMediaSource.setUsername("otter"); dbMediaSource.setPassword("otter"); dbMediaSource.setUrl("jdbc:mysql://127.0.0.1:3306/retl"); dbMediaSource.setEncode("UTF-8"); dbMediaSource.setType(DataMediaType.MYSQL); DbDataMedia dataMedia = new DbDataMedia(); dataMedia.setSource(dbMediaSource); dataMedia.setId(1L); dataMedia.setName("ljhtable1"); dataMedia.setNamespace("otter"); final DbDialect dbDialect = dbDialectFactory.getDbDialect(2L, dataMedia.getSource()); want.object(dbDialect).clazIs(MysqlDialect.class); final TransactionTemplate transactionTemplate = dbDialect.getTransactionTemplate(); // ?? int minute = 5; int nextId = 1; final int thread = 10; final int batch = 50; final String sql = "insert into otter.ljhtable1 values(? , ? , ? , ?)"; final CountDownLatch latch = new CountDownLatch(thread); ExecutorService executor = new ThreadPoolExecutor(thread, thread, 60, TimeUnit.SECONDS, new ArrayBlockingQueue(thread * 2), new NamedThreadFactory("load"), new ThreadPoolExecutor.CallerRunsPolicy()); for (int sec = 0; sec < minute * 60; sec++) { // long startTime = System.currentTimeMillis(); for (int i = 0; i < thread; i++) { final int start = nextId + i * batch; executor.submit(new Runnable() { public void run() { try { transactionTemplate.execute(new TransactionCallback() { public Object doInTransaction(TransactionStatus status) { JdbcTemplate jdbcTemplate = dbDialect.getJdbcTemplate(); return jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() { public void setValues(PreparedStatement ps, int idx) throws SQLException { int id = start + idx; StatementCreatorUtils.setParameterValue(ps, 1, Types.INTEGER, null, id); StatementCreatorUtils.setParameterValue(ps, 2, Types.VARCHAR, null, RandomStringUtils.randomAlphabetic(1000)); // RandomStringUtils.randomAlphabetic() long time = new Date().getTime(); StatementCreatorUtils.setParameterValue(ps, 3, Types.TIMESTAMP, new Timestamp(time)); StatementCreatorUtils.setParameterValue(ps, 4, Types.TIMESTAMP, new Timestamp(time)); } public int getBatchSize() { return batch; } }); } }); } finally { latch.countDown(); } } }); } long endTime = System.currentTimeMillis(); try { latch.await(1000 * 60L - (endTime - startTime), TimeUnit.MILLISECONDS); } catch (InterruptedException e) { e.printStackTrace(); } if (latch.getCount() != 0) { System.out.println("perf is not enough!"); System.exit(-1); } endTime = System.currentTimeMillis(); System.out.println("Time cost : " + (System.currentTimeMillis() - startTime)); try { TimeUnit.MILLISECONDS.sleep(1000L - (endTime - startTime)); } catch (InterruptedException e) { e.printStackTrace(); } nextId = nextId + thread * batch; } executor.shutdown(); }
From source file:com.smartitengineering.dao.hbase.autoincrement.AutoIncrementRowIdForLongTest.java
@Test public void testMultithreadedKeysSelfProvided() throws Exception { ExecutorService service = Executors.newFixedThreadPool(THREAD_COUNT); final long start = 3102; final int bound = THREAD_COUNT; final int innerBound = 30; List<Future> futures = new ArrayList<Future>(); final long startTime = System.currentTimeMillis(); for (int i = 0; i < bound; ++i) { final int index = i; futures.add(service.submit(new Runnable() { @Override// w w w .j a v a 2s .c o m public void run() { for (int j = 0; j < innerBound; ++j) { final HTableInterface table = pool.getTable(TABLE_NAME); long id = index * bound + j + start; final long id1 = Long.MAX_VALUE - id; synchronized (ids) { final long mainId = Long.MAX_VALUE - id1; Assert.assertFalse(ids.contains(mainId)); ids.add(mainId); } final byte[] row = Bytes.toBytes(id1); Put put = new Put(row); final byte[] toBytes = Bytes.toBytes("value " + id); put.add(FAMILY_BYTES, CELL_BYTES, toBytes); Result get1; try { table.put(put); Get get = new Get(row); get1 = table.get(get); } catch (Exception ex) { LOGGER.error(ex.getMessage(), ex); get1 = null; Assert.fail(ex.getMessage()); } pool.putTable(table); Assert.assertNotNull(get1); Assert.assertFalse(get1.isEmpty()); Assert.assertTrue(Arrays.equals(row, get1.getRow())); Assert.assertTrue(Arrays.equals(toBytes, get1.getValue(FAMILY_BYTES, CELL_BYTES))); } } })); } for (Future future : futures) { future.get(); } final long endTime = System.currentTimeMillis(); LOGGER.info("Time for " + (bound * innerBound) + " rows ID retrieval, put and get is " + (endTime - startTime) + "ms"); Assert.assertEquals(bound * innerBound + start - 1, ids.size()); }
From source file:com.linemetrics.monk.api.ApiClient.java
public List<DataItem> getRangeOptimized(final Number dataStreamId, long time_from, long time_to, TDB tdb, TimeZone tz) throws ApiException { try {// www .j av a 2 s .c o m long timeDiff = time_to - time_from; long maxTimeRange = tdb.getQueryLimit(); long queryRange = tdb.getQueryRange(); if (timeDiff < maxTimeRange) { return this.getRange(dataStreamId, time_from, time_to, tdb, tz); } long millis = System.currentTimeMillis(); ExecutorService executorService = Executors.newSingleThreadExecutor(); Set<Future<List<DataItem>>> callables = new HashSet<Future<List<DataItem>>>(); long queryStart = time_from; long queryEnd = time_from + queryRange; while (queryStart < time_to) { callables.add(executorService .submit(new CallableRangeQuery(dataStreamId, queryStart, queryEnd, tdb, tz))); queryStart += queryRange; queryEnd += queryRange; } executorService.shutdown(); List<DataItem> list = new ArrayList<>(); for (Future<List<DataItem>> future : callables) { List<DataItem> slice = future.get(); if (slice == null) { throw new ApiException("Error while retrieving slice :("); } else { list.addAll(slice); } } executorService.awaitTermination(60 * 60 * 1000L, TimeUnit.MILLISECONDS); System.out.print("Optimized Range Query takes: " + (System.currentTimeMillis() - millis) + "ms "); // System.out.println(list.size()); // Collections.sort(list, DataItemComparator.getInstance()); DataItem prevItem = null, currItem; DataItem beginSlice = null; Iterator<DataItem> itemIterator = list.iterator(); while (itemIterator.hasNext()) { currItem = itemIterator.next(); if (prevItem != null) { if (prevItem.getTimestamp().equals(currItem.getTimestamp())) { itemIterator.remove(); continue; } if (beginSlice == null) { if (currItem.getTimestamp() - prevItem.getTimestamp() > tdb.getMilliseconds()) { beginSlice = prevItem; } } else { if (currItem.getTimestamp() - prevItem.getTimestamp() == tdb.getMilliseconds()) { System.out.println("TimeRange " + beginSlice.getTimestamp() + " - " + prevItem.getTimestamp() + " " + (prevItem.getTimestamp() - beginSlice.getTimestamp()) + " ms missing!"); beginSlice = null; } } } prevItem = currItem; } if (beginSlice != null) { System.out.println("TimeRange " + beginSlice.getTimestamp() + " - " + prevItem.getTimestamp() + " " + (prevItem.getTimestamp() - beginSlice.getTimestamp()) + " ms missing!"); } long expectedItems = ((time_to - time_from) / tdb.getMilliseconds()) - 1; System.out.println(" (" + (list.size() - expectedItems) + ")"); return list; } catch (Exception e) { throw new ApiException(e.getMessage()); } }
From source file:com.playhaven.android.view.HTMLView.java
/** * This switches on the host portion of a request prefixed with * DISPATCH_PREFIX in order to handle events from the content templates. * * @TODO this would be a good candidate for factoring out to a cleaner custom WebViewClient * * @param dispatchUrl//from w w w.ja v a 2 s . co m */ private void handleDispatch(String dispatchUrl) { Uri callbackUri = Uri.parse(dispatchUrl); String callbackId = callbackUri.getQueryParameter("callback"); String callbackString = callbackUri.getHost(); String dispatchContext = callbackUri.getQueryParameter("context"); PlayHaven.d("Handling dispatch: %s of type %s", dispatchUrl, callbackString); switch (Dispatches.valueOf(callbackString)) { /** * closeButton hides the native emergency close button, and passes * notice of whether it was hidden back to the content template */ case closeButton: String hidden = "true"; try { hidden = new JSONObject(dispatchContext).getString("hidden"); } catch (JSONException jse) { // Default to NOT hiding the emergency close button hidden = "false"; } if ("true".equals(hidden)) { ((PlayHavenView) getParent()).setExitVisible(false); } // Tell the content template that we've hidden the emergency close button. this.loadUrl(String.format(CALLBACK_TEMPLATE, callbackId, "{'hidden':'" + hidden + "'}", null)); break; /** * dismiss triggers the contentDismissed listener */ case dismiss: PlayHavenView.DismissType dismiss = PlayHavenView.DismissType.NoThanks; if (mRewards != null) dismiss = PlayHavenView.DismissType.Reward; if (mDataFields != null) dismiss = PlayHavenView.DismissType.OptIn; if (mPurchases != null) dismiss = PlayHavenView.DismissType.Purchase; mPlacement.getListener().contentDismissed(mPlacement, dismiss, generateResponseBundle()); // Unregister the web view client so that any future dispatches will be ignored. HTMLView.this.setWebViewClient(null); break; /** * launch retrieves a URL from the server to be parsed using * Intent.ACTION_VIEW */ case launch: mPlacement.getListener().contentDismissed(mPlacement, PlayHavenView.DismissType.Launch, null); /* * We can't get this from the original model because we don't * know which one they picked (if this was a more_games template). */ String url; try { url = new JSONObject(dispatchContext).getString("url"); } catch (JSONException jse) { PlayHaven.e("Could not parse launch URL."); return; } UrlRequest urlRequest = new UrlRequest(url); ExecutorService pool = Executors.newSingleThreadExecutor(); final Future<String> uriFuture = pool.submit(urlRequest); final String initialUrl = url; new Thread(new Runnable() { @Override public void run() { // Wait for our final link. String url = null; try { url = uriFuture.get(); } catch (Exception e) { PlayHaven.v("Could not retrieve launch URL from server. Using initial url."); // If the redirect failed, proceed with the original url. url = initialUrl; } // Launch whatever it is. It might be a Play, web, or other link Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url)); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); try { HTMLView.this.getContext().startActivity(intent); } catch (Exception e) { PlayHaven.e("Unable to launch URI from template."); e.printStackTrace(); } } }).start(); break; /** * loadContext passes the full "context" JSON blob to the * content template */ case loadContext: this.loadUrl(DISPATCH_PROTOCOL_TEMPLATE); net.minidev.json.JSONObject context = JsonUtil.getPath(mPlacement.getModel(), "$.response.context"); /** * @playhaven.apihack KitKat+ changed how the webview is loaded */ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) { this.evaluateJavascript(String.format(CALLBACK_TEMPLATE, callbackId, context, null), null); } else { this.loadUrl(String.format(CALLBACK_TEMPLATE, callbackId, context, null)); } break; /** * purchase stores the purchase object (which is generated by the * content template) as mPurchases, for use with dismiss dispatch */ case purchase: collectAttachments(dispatchContext); break; /** * reward stores the reward object (which is generated by the * content template) as mRewards, for use with dismiss dispatch */ case reward: net.minidev.json.JSONObject rewardParam = JsonUtil.getPath(mPlacement.getModel(), "$.response.context.content.open_dispatch.parameters"); if (rewardParam == null || rewardParam.size() == 0) { // data_collection template sends a reward dispatch when it submits form data ... // @TODO: have templates return more than key/value pairs (eg class, pattern) this.loadUrl(COLLECT_FORM_DATA); } collectAttachments(dispatchContext); break; /** * subcontent takes a JSON blob generated by the content template * and uses that to get data for a new impression, currently a * more_games widget that follows a featured ad */ case subcontent: SubcontentRequest subcontentRequest = new SubcontentRequest(dispatchContext); subcontentRequest.send(getContext()); break; /** @TODO Find out why this dispatch was abandoned in 1.12 */ case track: PlayHaven.d("track callback not implemented."); break; /** * This is one injected to let the Android SDK harvest data from the * opt-in data collection form. */ case dcData: try { mDataFields = DataCollectionField.fromUrl(callbackUri); } catch (PlayHavenException e) { e.printStackTrace(); } break; default: break; } }