Java Code Examples for java.util.concurrent.ExecutorService#execute()
The following examples show how to use
java.util.concurrent.ExecutorService#execute() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: loginServerThread.java From fei_Q with GNU General Public License v2.0 | 6 votes |
public void run() { ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * POOL_SIZE); while (true) { try { // SimpleDateFormat sf = new SimpleDateFormat("yy-MM-dd hh:mm:ss"); // System.out.println("At the time " + sf.format(new Date()) + " loginServer support the " + times + "th time service to" + client.getInetAddress().toString().substring(1)); // times++; client = server.accept(); login_dealingThread dt = new login_dealingThread(client); executorService.execute(dt); // System.out.println("loginServer go on!"); } catch (Exception e) { e.printStackTrace(); } } }
Example 2
Source File: ArrayBlockingQueueTest.java From j2objc with Apache License 2.0 | 6 votes |
/** * offer transfers elements across Executor tasks */ public void testOfferInExecutor() { final ArrayBlockingQueue q = new ArrayBlockingQueue(2); q.add(one); q.add(two); final CheckedBarrier threadsStarted = new CheckedBarrier(2); final ExecutorService executor = Executors.newFixedThreadPool(2); try (PoolCleaner cleaner = cleaner(executor)) { executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { assertFalse(q.offer(three)); threadsStarted.await(); assertTrue(q.offer(three, LONG_DELAY_MS, MILLISECONDS)); assertEquals(0, q.remainingCapacity()); }}); executor.execute(new CheckedRunnable() { public void realRun() throws InterruptedException { threadsStarted.await(); assertEquals(0, q.remainingCapacity()); assertSame(one, q.take()); }}); } }
Example 3
Source File: ExecutorsDemo.java From JavaCommon with Apache License 2.0 | 6 votes |
static void demo() { ExecutorService cachedThreadPool = Executors.newCachedThreadPool(); for (int i = 0; i < 10; i++) { final int index = i; try { Thread.sleep(index * 1000); } catch (InterruptedException e) { e.printStackTrace(); } cachedThreadPool.execute(new Runnable() { public void run() { System.out.println(index); } }); } }
Example 4
Source File: LiveEventBusDemo.java From LiveEventBus with Apache License 2.0 | 6 votes |
public void postValueCountTest() { sendCount = 1000; receiveCount = 0; ExecutorService threadPool = Executors.newFixedThreadPool(2); for (int i = 0; i < sendCount; i++) { threadPool.execute(new Runnable() { @Override public void run() { LiveEventBus.get(KEY_TEST_MULTI_THREAD_POST).post("test_data"); } }); } new Handler().postDelayed(new Runnable() { @Override public void run() { Toast.makeText(LiveEventBusDemo.this, "sendCount: " + sendCount + " | receiveCount: " + receiveCount, Toast.LENGTH_LONG).show(); } }, 1000); }
Example 5
Source File: LiveEventBusDemo.java From LiveEventBus with Apache License 2.0 | 6 votes |
public void postValueCountTest() { sendCount = 1000; receiveCount = 0; ExecutorService threadPool = Executors.newFixedThreadPool(2); for (int i = 0; i < sendCount; i++) { threadPool.execute(new Runnable() { @Override public void run() { LiveEventBus.get().with(KEY_TEST_MULTI_THREAD_POST).postValue("test_data"); } }); } new Handler().postDelayed(new Runnable() { @Override public void run() { Toast.makeText(LiveEventBusDemo.this, "sendCount: " + sendCount + " | receiveCount: " + receiveCount, Toast.LENGTH_LONG).show(); } }, 1000); }
Example 6
Source File: SharedBufferTest2.java From dctb-utfpr-2018-1 with Apache License 2.0 | 6 votes |
public static void main(String[] args) { // create new thread pool with two threads ExecutorService application = Executors.newFixedThreadPool(2); // create SynchronizedBuffer to store ints Buffer sharedLocation = new SynchronizedBuffer(); System.out.printf("%-40s%s\t\t%s\n%-40s%s\n\n", "Operation", "Buffer", "Occupied", "---------", "------\t\t--------"); try // try to start producer and consumer { application.execute(new Producer(sharedLocation)); application.execute(new Consumer(sharedLocation)); } // end try catch (Exception exception) { exception.printStackTrace(); } // end catch application.shutdown(); }
Example 7
Source File: CoreExecutorService.java From jumbune with GNU Lesser General Public License v3.0 | 6 votes |
/** * This method cleans up the job name folder in temp directory on slaves. * * @param config the config * @throws JumbuneException the hTF exception * @throws IOException Signals that an I/O exception has occurred. * @throws InterruptedException the interrupted exception */ protected static void cleanUpSlavesTempFldr(JumbuneRequest jumbuneRequest) throws JumbuneException, IOException, InterruptedException { Cluster cluster = jumbuneRequest.getCluster(); ExecutorService cleanUpSlavesService = null; LOGGER.debug("Starting clean up process................"); try { List<String> workerHosts = cluster.getWorkers().getHosts(); cleanUpSlavesService = Executors.newFixedThreadPool(workerHosts.size()); for (String workerHost : workerHosts) { CleanUpSlaves cleanUpSlaves = new CleanUpSlaves(jumbuneRequest, workerHost); cleanUpSlavesService.execute(cleanUpSlaves); } } catch (Exception e) { LOGGER.error(JumbuneRuntimeException.throwException(e.getStackTrace())); } finally { cleanUpSlavesService.shutdown(); } }
Example 8
Source File: Merge.java From hbase-tools with Apache License 2.0 | 5 votes |
private List<HRegionInfo> findEmptyRegionsInternal(TableInfo tableInfo) throws Exception { long timestamp = System.currentTimeMillis(); Set<HRegionInfo> emptyRegions = Collections.synchronizedSet(Collections.newSetFromMap(new TreeMap<HRegionInfo, Boolean>())); tableInfo.refresh(); ExecutorService executorService = Executors.newFixedThreadPool(EmptyRegionChecker.THREAD_POOL_SIZE); try { EmptyRegionChecker.resetCounter(); Set<HRegionInfo> allTableRegions = tableInfo.getRegionInfoSet(); for (HRegionInfo regionInfo : allTableRegions) { RegionLoadDelegator regionLoad = tableInfo.getRegionLoad(regionInfo); if (regionLoad == null) { Util.printMessage("RegionLoad is empty - " + regionInfo); throw new IllegalStateException(Constant.MESSAGE_NEED_REFRESH); } if (regionLoad.getStorefileSizeMB() == 0 && regionLoad.getMemStoreSizeMB() == 0) { executorService.execute( new EmptyRegionChecker(connection, tableInfo.getTableName(), regionInfo, emptyRegions)); } } } finally { executorService.shutdown(); executorService.awaitTermination(30, TimeUnit.MINUTES); } Util.printMessage(emptyRegions.size() + " empty regions are found."); Util.printVerboseMessage(args, Util.getMethodName(), timestamp); return new ArrayList<>(emptyRegions); }
Example 9
Source File: ProxyRace.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) throws Exception { Phaser phaser = new Phaser(threads) { @Override protected boolean onAdvance(int phase, int registeredParties) { // install new ClassLoader on each advance classLoader = new CL(); return terminate; } }; ExecutorService exe = Executors.newFixedThreadPool(threads); for (int i = 0; i < threads; i++) { exe.execute(() -> { while (phaser.arriveAndAwaitAdvance() >= 0) { Class<?> proxyClass = Proxy.getProxyClass(classLoader, Runnable.class); if (!Proxy.isProxyClass(proxyClass)) { racesDetected.incrementAndGet(); } } }); } Thread.sleep(5000L); terminate = true; exe.shutdown(); exe.awaitTermination(5L, TimeUnit.SECONDS); System.out.println(racesDetected.get() + " races detected"); if (racesDetected.get() != 0) { throw new RuntimeException(racesDetected.get() + " races detected"); } }
Example 10
Source File: ClientSocket.java From javabase with Apache License 2.0 | 5 votes |
public static void main(String[] args) { ExecutorService pool = Executors.newFixedThreadPool(clientSize); for (int i = 0; i < clientSize; i++) { pool.execute(clientThread(i)); } pool.shutdown(); }
Example 11
Source File: SynchronizedHashMapWithRWLock.java From tutorials with MIT License | 5 votes |
public static void main(String[] args) throws InterruptedException { final int threadCount = 3; final ExecutorService service = Executors.newFixedThreadPool(threadCount); SynchronizedHashMapWithRWLock object = new SynchronizedHashMapWithRWLock(); service.execute(new Thread(new Writer(object), "Writer")); service.execute(new Thread(new Reader(object), "Reader1")); service.execute(new Thread(new Reader(object), "Reader2")); service.shutdown(); }
Example 12
Source File: GridHandler.java From kurento-java with Apache License 2.0 | 5 votes |
public synchronized void filterValidNodes() { if (!nodeListFiltered) { log.debug("Node availables in the node list: {}", nodeList.size()); int nodeListSize = nodeList.size(); ExecutorService executor = Executors.newFixedThreadPool(nodeListSize); final CountDownLatch latch = new CountDownLatch(nodeListSize); for (final String nodeCandidate : nodeList) { executor.execute(new Runnable() { @Override public void run() { if (!nodeIsValid(nodeCandidate)) { nodeList.remove(nodeCandidate); } latch.countDown(); } }); } try { latch.await(); } catch (InterruptedException e) { throw new RuntimeException(e); } nodeListFiltered = true; log.debug("Node availables in the node list after filtering: {}", nodeList.size()); } }
Example 13
Source File: WikipediaEditsSourceTest.java From flink with Apache License 2.0 | 4 votes |
/** * We first check the connection to the IRC server. If it fails, this test is ignored. */ @Test @RetryOnFailure(times = 1) public void testWikipediaEditsSource() throws Exception { if (canConnect(1, TimeUnit.SECONDS)) { final Time testTimeout = Time.seconds(60); final WikipediaEditsSource wikipediaEditsSource = new WikipediaEditsSource(); ExecutorService executorService = null; try { executorService = Executors.newSingleThreadExecutor(); BlockingQueue<Object> collectedEvents = new ArrayBlockingQueue<>(1); AtomicReference<Exception> asyncError = new AtomicReference<>(); // Execute the source in a different thread and collect events into the queue. // We do this in a separate thread in order to not block the main test thread // indefinitely in case that something bad happens (like not receiving any // events) executorService.execute(() -> { try { wikipediaEditsSource.run(new CollectingSourceContext<>(collectedEvents)); } catch (Exception e) { boolean interrupted = e instanceof InterruptedException; if (!interrupted) { LOG.warn("Failure in WikipediaEditsSource", e); } asyncError.compareAndSet(null, e); } }); long deadline = deadlineNanos(testTimeout); Object event = null; Exception error = null; // Check event or error while (event == null && error == null && System.nanoTime() < deadline) { event = collectedEvents.poll(1, TimeUnit.SECONDS); error = asyncError.get(); } if (error != null) { // We don't use assertNull, because we want to include the error message fail("Failure in WikipediaEditsSource: " + error.getMessage()); } assertNotNull("Did not receive a WikipediaEditEvent within the desired timeout", event); assertTrue("Received unexpected event " + event, event instanceof WikipediaEditEvent); } finally { wikipediaEditsSource.cancel(); if (executorService != null) { executorService.shutdownNow(); executorService.awaitTermination(1, TimeUnit.SECONDS); } } } else { LOG.info("Skipping test, because not able to connect to IRC server."); } }
Example 14
Source File: ApiRateLimitTest.java From cosmic with Apache License 2.0 | 4 votes |
@Test public void multipleClientsCanAccessWithoutBlocking() throws Exception { final int allowedRequests = 200; s_limitService.setMaxAllowed(allowedRequests); s_limitService.setTimeToLive(1); final User key = createFakeUser(); final int clientCount = allowedRequests; final Runnable[] clients = new Runnable[clientCount]; final boolean[] isUsable = new boolean[clientCount]; final CountDownLatch startGate = new CountDownLatch(1); final CountDownLatch endGate = new CountDownLatch(clientCount); for (int i = 0; i < isUsable.length; ++i) { final int j = i; clients[j] = new Runnable() { /** * {@inheritDoc} */ @Override public void run() { try { startGate.await(); isUsable[j] = isUnderLimit(key); } catch (final InterruptedException e) { e.printStackTrace(); } finally { endGate.countDown(); } } }; } final ExecutorService executor = Executors.newFixedThreadPool(clientCount); for (final Runnable runnable : clients) { executor.execute(runnable); } startGate.countDown(); endGate.await(); for (final boolean b : isUsable) { assertTrue("Concurrent client request should be allowed within limit", b); } }
Example 15
Source File: App.java From hadoop-arch-book with Apache License 2.0 | 4 votes |
private static void runEventTest(final int numberOfUsers, int numberOfEvents, int numberOfThreads, String cacheServiceType, String hbaseServiceType) { final AbstractFraudHBaseService hbaseService = ServiceFactory.initHBaseService(hbaseServiceType); final AbstractCacheFraudService cacheService = ServiceFactory.initCacheService(cacheServiceType, hbaseService); ExecutorService executorService = Executors.newFixedThreadPool(numberOfThreads); final AtomicLong alertCount = new AtomicLong(); final AtomicLong eventCount = new AtomicLong(); for (int i = 0; i < numberOfEvents/5; i++) { executorService.execute(new Runnable() { public void run() { Random r = new Random(); String ipAddress; if (r.nextInt(100) == 50) { ipAddress = TestConsts.UNSAFE_UP_ADDRESS_LIST.get(r.nextInt(TestConsts.UNSAFE_UP_ADDRESS_LIST.size())); } else { ipAddress = TestConsts.SAFE_UP_ADDRESS_LIST.get(r.nextInt(TestConsts.SAFE_UP_ADDRESS_LIST.size())); } try { int buyerId = r.nextInt(numberOfUsers); int sellerId = r.nextInt(numberOfUsers); cacheService.logInProfileInHBase(r.nextInt(numberOfUsers),ipAddress); int eventsToDo = r.nextInt(10); for (int i = 0; i < eventsToDo; i++) { eventCount.addAndGet(1); long itemValue; if (r.nextInt(50) == 25) { itemValue = r.nextInt(100000); } else { itemValue = r.nextInt(10000); } ItemSaleEvent event = new ItemSaleEvent(buyerId, sellerId, r.nextLong(), itemValue); List<Alert> alerts = cacheService.processEventForAlerts(event); alertCount.addAndGet(alerts.size()); } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.print("."); } }); } System.out.println("Total Events:" + eventCount.get()); System.out.println("Total Alerts:" + alertCount.get()); System.out.println("Total LogIns:" + numberOfEvents/5); }
Example 16
Source File: IPv6InMemoryAuthTicketsHelperConcurrencyTest.java From p4ic4idea with Apache License 2.0 | 4 votes |
/** * Test in-memory tickets - saving auth tickets */ @Test public void testInMemorySaveAuthTicketsConcurrently() { String address = "[fc01:5034:a05:1e:ad94:403f:ae19:1aa9]:1666"; String value = "ABCDEF123123"; String user = "bruno"; String ticketsFilePath = null; try { // Create the first ticket AuthTicketsHelper.saveTicket(user, address, value, ticketsFilePath); // Run concurrent reads and writes ExecutorService executor = Executors.newFixedThreadPool(10); for (int i = 0; i < 25; i++) { String addr = address + i; String val = value + i; String usr = user + i; Runnable task = null; if ((i % 2) == 0) { task = new AuthTicketsWriter(usr, addr, val, ticketsFilePath); } else { task = new AuthTicketsReader(ticketsFilePath); } executor.execute(task); } executor.shutdown(); while (!executor.isTerminated()) { System.out.println("Threads are still running..."); Thread.sleep(2000); } System.out.println("Finished all threads"); // Check the size of the in-memory map - 501 AuthTicket[] tickets = AuthTicketsHelper.getTickets(ticketsFilePath); assertNotNull(tickets); assertTrue(tickets.length > 10); } catch (Exception exc) { fail("Unexpected exception: " + exc.getLocalizedMessage()); } }
Example 17
Source File: PerformanceCli.java From waltz with Apache License 2.0 | 4 votes |
@Override protected void processCmd(CommandLine cmd) throws SubCommandFailedException { try { // check required argument txnSize = Integer.parseInt(cmd.getOptionValue("txn-size")); if (txnSize < 0) { throw new IllegalArgumentException("Found negative: txn-size must be equal or greater than 0"); } data = new byte[txnSize]; numTxn = Integer.parseInt(cmd.getOptionValue("num-txn")); if (numTxn < 0) { throw new IllegalArgumentException("Found negative: num-txn must be greater or equal to 0"); } allTxnReceived = new CountDownLatch(numTxn); allTxnRead = new CountDownLatch(numTxn); waltzClientConfig = getWaltzClientConfig(cmd.getOptionValue("cli-config-path")); // check optional argument if (cmd.hasOption("num-active-partitions")) { numActivePartitions = Integer.parseInt(cmd.getOptionValue("num-active-partitions")); if (numActivePartitions < 1) { throw new IllegalArgumentException("Number of active partitions must be greater of equals to 1"); } } // produce transactions to consume // since we only care about consumer performance, we can create // transactions with multiple producer to expedite the test int numThread = numTxn > DEFAULT_NUM_PRODUCERS ? DEFAULT_NUM_PRODUCERS : numTxn; int txnPerThread = numTxn / numThread; ExecutorService executor = Executors.newFixedThreadPool(numThread); for (int i = 0; i < numThread; i++) { ProducerCallbacks callbacks = new ProducerCallbacks(); WaltzClient client = new WaltzClient(callbacks, waltzClientConfig); executor.execute(new ProducerThread(txnPerThread, client)); } // consume transactions when all committed new ConsumerThread(waltzClientConfig).run(); // wait until all transaction consumed allTxnRead.await(); printStatistic(); executor.shutdown(); } catch (Exception e) { throw new SubCommandFailedException(String.format("Failed to run consumer performance test: %s", e.getMessage())); } }
Example 18
Source File: EmbeddedExclusiveSpiedThroughput.java From aeron with Apache License 2.0 | 4 votes |
public static void main(final String[] args) throws Exception { loadPropertiesFiles(args); final RateReporter reporter = new RateReporter( TimeUnit.SECONDS.toNanos(1), EmbeddedExclusiveSpiedThroughput::printRate); final ExecutorService executor = Executors.newFixedThreadPool(2); final AtomicBoolean running = new AtomicBoolean(true); final MediaDriver.Context ctx = new MediaDriver.Context() .spiesSimulateConnection(true); try (MediaDriver ignore = MediaDriver.launch(ctx); Aeron aeron = Aeron.connect(); Subscription subscription = aeron.addSubscription(CommonContext.SPY_PREFIX + CHANNEL, STREAM_ID); ExclusivePublication publication = aeron.addExclusivePublication(CHANNEL, STREAM_ID)) { executor.execute(reporter); executor.execute(() -> SamplesUtil.subscriberLoop( rateReporterHandler(reporter), FRAGMENT_COUNT_LIMIT, running).accept(subscription)); final ContinueBarrier barrier = new ContinueBarrier("Execute again?"); final IdleStrategy idleStrategy = SampleConfiguration.newIdleStrategy(); do { System.out.format( "%nStreaming %,d messages of payload length %d bytes to %s on stream id %d%n", NUMBER_OF_MESSAGES, MESSAGE_LENGTH, CHANNEL, STREAM_ID); printingActive = true; long backPressureCount = 0; for (long i = 0; i < NUMBER_OF_MESSAGES; i++) { OFFER_BUFFER.putLong(0, i); idleStrategy.reset(); while (publication.offer(OFFER_BUFFER, 0, MESSAGE_LENGTH, null) < 0) { backPressureCount++; idleStrategy.idle(); } } System.out.println( "Done streaming. backPressureRatio=" + ((double)backPressureCount / NUMBER_OF_MESSAGES)); if (LINGER_TIMEOUT_MS > 0) { System.out.println("Lingering for " + LINGER_TIMEOUT_MS + " milliseconds..."); Thread.sleep(LINGER_TIMEOUT_MS); } printingActive = false; } while (barrier.await()); running.set(false); reporter.halt(); executor.shutdown(); } }
Example 19
Source File: StatisticsServiceImpl.java From pacbot with Apache License 2.0 | 4 votes |
public List<Map<String, Object>> getStats() throws Exception { try { List<Map<String, Object>> statsList = new ArrayList<>(); Map<String, Object> data = new HashMap<>(); data.put("totalAutoFixesApplied", getAutofixStats().get(0).get(COUNT)); Long totalAssets = getTotalAssetCount(); Long eventsProcessed = getTotalEventProcessed(); Map<String, Long> violationsMap = getIssueDistribution(); String targettypes = repository.getTargetTypeForAG(MASTER_ALIAS, null); ExecutorService executor = Executors.newCachedThreadPool(); executor.execute(() -> { numberOfPoliciesEnforced = getNumberOfPoliciesEnforced(targettypes); }); executor.execute(() -> { numberOfAwsAccounts = getNumberOfAwsAccounts(); }); executor.execute(() -> { numberOfPolicyEvaluations = getNumberOfPolicyEvaluations(); }); executor.execute(() -> { numberOfPolicyWithAutoFixes = getNumberOfPolicyWithAutoFixes(); }); executor.shutdown(); while (!executor.isTerminated()) { } // 1. Total number of policies active in AWS Datasources data.put("numberOfPoliciesEnforced", numberOfPoliciesEnforced); // 2.Total Accounts data.put("numberOfAwsAccounts", numberOfAwsAccounts); // 3.Total Assets Scanned data.put("totalNumberOfAssets", totalAssets); // 4.Total Events Processed data.put("numberOfEventsProcessed", eventsProcessed); // 5. No of polices Evaluated from Fre-stats data.put("numberOfPolicyEvaluations", numberOfPolicyEvaluations); // 6. Auto Fix data.put("numberOfPolicyWithAutoFixes", numberOfPolicyWithAutoFixes); //data.put("totalAutoFixesApplied", totalAutofixapplied); data.put("totalViolations", violationsMap); // 6. Stats /* * String overAllComplianceData = * complianceClient.getOverallCompliance(AWS, "Infra & Platforms") * .get("data").toString(); JsonObject responseDetailsjson = * parser.parse(overAllComplianceData).getAsJsonObject(); Type type * = new TypeToken<Map<String, String>>() { }.getType(); * * data.put("compliance", * gson.fromJson(responseDetailsjson.get("distribution"), type)); */ statsList.add(data); return statsList; } catch (ServiceException e) { LOGGER.error("Error @ StatisticsServiceImpl/getStats", e); throw new ServiceException(e); } }
Example 20
Source File: LoadGenerator.java From camunda-bpm-platform with Apache License 2.0 | 4 votes |
private void runSetup(ExecutorService executorService) throws InterruptedException { CountDownLatch sync = new CountDownLatch(configuration.getSetupTasks().length); System.out.print("Running setup ... "); for (Runnable r : configuration.getSetupTasks()) { executorService.execute(wrap(r, sync)); } sync.await(); if(configuration.isColor()) System.out.print(ANSI_GREEN); System.out.println("Done"); if(configuration.isColor()) System.out.print(ANSI_RESET); }