private void callParserThread( String[] sources, String[] encodings, String[] classPaths, ArrayList<String> acceptedFiles, ArrayList<String> sharedRevisionEntryList, Multimap<String, String> invokers) throws IOException { try { int numOfThreads = Runtime.getRuntime().availableProcessors(); BlockingQueue<String> toProcess = new ArrayBlockingQueue<String>(acceptedFiles.size(), false, acceptedFiles); ExecutorService executorService = Executors.newFixedThreadPool(numOfThreads); for (int i = 0; i < numOfThreads; ++i) { ParserRunnable runnerParser = new ParserRunnable( sources, encodings, classPaths, toProcess, sharedRevisionEntryList, invokers, numOfThreads); executorService.submit(runnerParser); } executorService.shutdown(); executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS); } catch (InterruptedException e) { e.printStackTrace(); } }
public static void main(String[] args) { final BlockingQueue<Character> bq; bq = new ArrayBlockingQueue<Character>(26); final ExecutorService executor = Executors.newFixedThreadPool(2); Runnable producer = () -> { for (char ch = 'A'; ch <= 'Z'; ch++) { try { bq.put(ch); System.out.printf("%c produced by " + "producer.%n", ch); } catch (InterruptedException ie) { } } }; executor.execute(producer); Runnable consumer = () -> { char ch = '\0'; do { try { ch = bq.take(); System.out.printf("%c consumed by " + "consumer.%n", ch); } catch (InterruptedException ie) { } } while (ch != 'Z'); executor.shutdownNow(); }; executor.execute(consumer); }
@Test public void testNoApiLimitOnRootAdmin() throws Exception { // issue list Accounts calls final HashMap<String, String> params = new HashMap<String, String>(); params.put("response", "json"); params.put("listAll", "true"); params.put("sessionkey", sessionKey); // assuming ApiRateLimitService set api.throttling.max = 25 int clientCount = 26; 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(); sendRequest("listAccounts", params); isUsable[j] = true; } catch (CloudRuntimeException e) { isUsable[j] = false; e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } finally { endGate.countDown(); } } }; } ExecutorService executor = Executors.newFixedThreadPool(clientCount); for (Runnable runnable : clients) { executor.execute(runnable); } startGate.countDown(); endGate.await(); int rejectCount = 0; for (int i = 0; i < isUsable.length; ++i) { if (!isUsable[i]) rejectCount++; } assertEquals("No request should be rejected!", 0, rejectCount); }
public void stop(final StopContext stopContext) { final ServiceController<?> serviceController = stopContext .getController() .getServiceContainer() .getService(CommonDeploymentService.getServiceName(jndiName)); if (serviceController != null) { serviceController.setMode(ServiceController.Mode.REMOVE); } ExecutorService executorService = executor.getValue(); Runnable r = new Runnable() { @Override public void run() { try { stopService(); } finally { stopContext.complete(); } } }; try { executorService.execute(r); } catch (RejectedExecutionException e) { r.run(); } finally { stopContext.asynchronous(); } }
private void findMachinesBasedOnLocalIp() { Optional<InetAddress> localhost = getLocalAddress(); if (localhost.isPresent()) { InetAddress inetAddress = localhost.get(); myIp = inetAddress.getAddress(); scanningThreadPool.submit( new Runnable() { @Override public void run() { if (!isCancelled()) { searchForIp(myIp); } } }); for (int i = 1; i <= MAX_IPS; i++) { final int finalI = i; scanningThreadPool.submit( new Runnable() { @Override public void run() { if (!isCancelled()) { searchForIp(createSubnetAddress(myIp, (byte) finalI)); } } }); } } }
public static void main(String[] args) { ExecutorService pool = Executors.newFixedThreadPool(2); final Vector[] vecs = { new Vector(), new Vector(), }; vecs[0].add(vecs[1]); vecs[1].add(vecs[0]); for (int i = 0; i < 2; i++) { final int threadNumber = i; pool.submit( new Callable() { public Object call() throws Exception { for (int i = 0; i < 1000 * 1000; i++) { ObjectOutputStream out = new ObjectOutputStream(new NullOutputStream()); out.writeObject(vecs[threadNumber]); out.close(); } System.out.println("done"); return null; } }); } }
private static ExecutorService startCommitAnnotationsClientService( SystemMain system, AtomicInteger jobCounter) { int configuredCount = Integer.valueOf( system.getConfiguration().getValue(SystemConfiguration.Property.CLIENT_THREADS)); int threadPoolCount = Math.max(configuredCount, 2); ExecutorService service = Executors.newFixedThreadPool( threadPoolCount, new ThreadFactory() { AtomicInteger id = new AtomicInteger(0); @Override public Thread newThread(Runnable r) { return new Thread( r, MessageFormat.format("annotationcommitclient-{0}", id.getAndIncrement())); } }); for (int i = 0; i < threadPoolCount; i++) { service.submit( new AnnotationCommitter( system.getServiceFactory().getCollectionService(), system.getServiceFactory().getMonitorService(), jobCounter)); } return service; }
private void testConcurrency(final CollectionHolder<List<Integer>> holder) throws InterruptedException, ExecutionException { final List<Integer> list = holder.collection; // make a big array that takes a long time to toString() list.addAll(LIST); // Create a thread pool with two threads to cause the most contention on the underlying // resource. final ExecutorService threadPool = Executors.newFixedThreadPool(2); // Consumes toStrings Callable<Integer> consumer = new Callable<Integer>() { public Integer call() { for (int i = 0; i < REPEAT; i++) { // Calls ToStringStyle new ToStringBuilder(holder).append(holder.collection); } return REPEAT; } }; Collection<Callable<Integer>> tasks = new ArrayList<Callable<Integer>>(); tasks.add(consumer); tasks.add(consumer); final List<Future<Integer>> futures = threadPool.invokeAll(tasks); for (Future<Integer> future : futures) { future.get(); } }
public static void shutdown() throws InterruptedException { excutor.shutdownNow(); while (!excutor.awaitTermination(5, TimeUnit.SECONDS)) { log.info("await for keyManager shutdown!"); } log.info("keyManager has shutdown!"); }
public void testSequence() throws Exception { final Seq seq = new Seq(); ExecutorService ex = Executors.newFixedThreadPool(5); List<Future> futures = ListUtil.newList(); for (int i = 0; i < 5; i++) { Future<Void> future = ex.submit( new Callable<Void>() { @Override public Void call() throws Exception { for (int i = 0; i < 100000000; i++) { seq.increase(); } return null; } }); futures.add(future); } for (Future f : futures) { f.get(); } assertEquals(5 * 100000000 + 1, seq.increase()); }
@Test public void testConfirmReceivedAfterPublisherCallbackChannelScheduleClose() throws Exception { final CountDownLatch latch = new CountDownLatch(40); templateWithConfirmsEnabled.setConfirmCallback( new ConfirmCallback() { @Override public void confirm(CorrelationData correlationData, boolean ack, String cause) { latch.countDown(); } }); ExecutorService executorService = Executors.newCachedThreadPool(); for (int i = 0; i < 20; i++) { executorService.execute( new Runnable() { @Override public void run() { templateWithConfirmsEnabled.convertAndSend( ROUTE, (Object) "message", new CorrelationData("abc")); templateWithConfirmsEnabled.convertAndSend( "BAD_ROUTE", (Object) "bad", new CorrelationData("cba")); } }); } assertTrue(latch.await(10, TimeUnit.SECONDS)); assertNull(templateWithConfirmsEnabled.getUnconfirmed(-1)); }
public void start() throws ExecutionException, InterruptedException { // Use the default MtGox settings Exchange mtGoxExchange = ExchangeFactory.INSTANCE.createExchange(MtGoxExchange.class.getName()); // Configure BTC/USD ticker stream for MtGox ExchangeStreamingConfiguration btcusdConfiguration = new MtGoxStreamingConfiguration(10, 10000, Currencies.BTC, Currencies.USD, false); // Interested in the public streaming market data feed (no authentication) StreamingExchangeService btcusdStreamingMarketDataService = mtGoxExchange.getStreamingExchangeService(btcusdConfiguration); // Requesting initial order book using the polling service PollingMarketDataService marketDataService = mtGoxExchange.getPollingMarketDataService(); MarketDataRunnable.book = marketDataService.getPartialOrderBook(Currencies.BTC, Currencies.USD); // Open the connections to the exchange btcusdStreamingMarketDataService.connect(); ExecutorService executorService = Executors.newSingleThreadExecutor(); Future<?> mtGoxMarketDataFuture = executorService.submit(new MarketDataRunnable(btcusdStreamingMarketDataService)); // the thread waits here until the Runnable is done. mtGoxMarketDataFuture.get(); executorService.shutdown(); // Disconnect and exit System.out.println(Thread.currentThread().getName() + ": Disconnecting..."); btcusdStreamingMarketDataService.disconnect(); }
public static void main(String[] args) throws InterruptedException, ExecutionException { List<Future<String>> futures = new ArrayList<>(); ExecutorService executor = Executors.newFixedThreadPool(5); for (int i = 1000; i <= 1010; i++) { futures.add(executor.submit(getTask(i))); } futures.add(executor.submit(getTask(10000000))); for (Future<String> future : futures) { System.out.println(future.get()); } executor.shutdown(); executor.awaitTermination(10, TimeUnit.SECONDS); /* output 500500 501501 502503 503506 504510 505515 506521 507528 508536 509545 510555 50000005000000 */ }
public static void main(String[] args) { Callable<Integer> task = () -> { try { TimeUnit.SECONDS.sleep(1); return 123; } catch (InterruptedException e) { throw new IllegalStateException("task interrupted", e); } }; ExecutorService executor = Executors.newFixedThreadPool(1); Future<Integer> future = executor.submit(task); System.out.println("future done? " + future.isDone()); Integer result = -1; try { result = future.get(); } catch (Exception e) { e.printStackTrace(); } System.out.println("future done? " + future.isDone()); System.out.println("result: " + result); stop(executor); }
protected synchronized void initializeQueue(String host, String queueName, Integer port) throws InterruptedException { final String bind = "tcp://" + host + ":" + port; _log.warn("binding to " + bind); if (_context == null) { _context = ZMQ.context(1); } if (_socket != null) { _executorService.shutdownNow(); _heartbeatService.shutdownNow(); Thread.sleep(1 * 1000); _log.warn("_executorService.isTerminated=" + _executorService.isTerminated()); _socket.close(); _executorService = Executors.newFixedThreadPool(1); _heartbeatService = Executors.newFixedThreadPool(1); } _socket = _context.socket(ZMQ.PUB); _socket.connect(bind); _executorService.execute(new SendThread(_socket, queueName)); _heartbeatService.execute(new HeartbeatThread(HEARTBEAT_INTERVAL)); _log.debug("Inference output queue is sending to " + bind); _initialized = true; }
@SuppressWarnings("finally") public boolean importUsers() { BufferedReader reader = null; try { FileInputStream fileInputStream = new FileInputStream(System.getenv("USERS_PATH")); InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8"); reader = new BufferedReader(inputStreamReader); String tempString = null; ExecutorService fixedThreadPool = Executors.newFixedThreadPool(100); while ((tempString = reader.readLine()) != null) { ImportUserThread t = new ImportUserThread(tempString); fixedThreadPool.execute(t); } reader.close(); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); return false; } finally { if (reader != null) { try { reader.close(); } catch (final IOException e) { e.printStackTrace(); return false; } } return true; } }
// The method executing the task @Override public void execute() throws BuildException { this.log("Starting PlantUML"); try { if (dir != null) { final File error = processingSingleDirectory(new File(dir)); eventuallyFailfast(error); } for (FileSet fileSet : filesets) { final File error = manageFileSet(fileSet); eventuallyFailfast(error); } for (FileList fileList : filelists) { final File error = manageFileList(fileList); eventuallyFailfast(error); } if (executorService != null) { executorService.shutdown(); executorService.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS); } this.log("Nb images generated: " + nbFiles.get()); } catch (IOException e) { e.printStackTrace(); throw new BuildException(e.toString()); } catch (InterruptedException e) { e.printStackTrace(); throw new BuildException(e.toString()); } }
public static void main(String[] args) throws Exception { if (args.length < 1) { System.out.println("java DownloadWeb <output dir>"); return; } BufferedReader reader = new BufferedReader(new InputStreamReader(System.in)); ExecutorService exec = Executors.newCachedThreadPool(); final String dir = args[0]; String url = null; while ((url = reader.readLine()) != null) { final URI uri = URI.create(url); String fileName = new File(uri.getPath()).getName(); final Path filePath = Paths.get(dir, fileName); exec.submit( new Runnable() { @Override public void run() { try (InputStream in = uri.toURL().openStream()) { Files.copy(in, filePath, StandardCopyOption.REPLACE_EXISTING); System.out.printf("completed: %s => %s\n", uri, filePath); } catch (Exception ex) { System.out.printf("failed: %s\n", uri); } } }); } exec.shutdown(); }
private static ExecutorService startAlertClientService( SystemMain system, AtomicInteger jobCounter) { int configuredCount = Integer.valueOf( system.getConfiguration().getValue(SystemConfiguration.Property.CLIENT_THREADS)); int configuredTimeout = Integer.valueOf( system .getConfiguration() .getValue(SystemConfiguration.Property.CLIENT_CONNECT_TIMEOUT)); int threadPoolCount = Math.max(configuredCount, 2); int timeout = Math.max(10000, configuredTimeout); ExecutorService service = Executors.newFixedThreadPool( threadPoolCount, new ThreadFactory() { AtomicInteger id = new AtomicInteger(0); @Override public Thread newThread(Runnable r) { return new Thread(r, MessageFormat.format("alertclient-{0}", id.getAndIncrement())); } }); system.getServiceFactory().getMonitorService().startRecordingCounters(); for (int i = 0; i < threadPoolCount; i++) { service.submit( new Alerter(system.getServiceFactory().getAlertService(), timeout, jobCounter)); } return service; }
/** {@inheritDoc} */ @Override public void onApplicationEvent(AgentMappingsUpdateEvent event) { Map<Long, AgentCacheEntry> agentCacheMap = nextGenInstrumentationManager.getAgentCacheMap(); // iterate all caches for (AgentCacheEntry agentCacheEntry : agentCacheMap.values()) { ConfigurationHolder configurationHolder = agentCacheEntry.getConfigurationHolder(); Environment cachedEnvironment = configurationHolder.getEnvironment(); PlatformIdent platformIdent = platformIdentDao.load(agentCacheEntry.getId()); try { // see what 's the new environment for the agent Environment environment = configurationResolver.getEnvironmentForAgent( platformIdent.getDefinedIPs(), platformIdent.getAgentName()); // fire job only if we have new environment or we were not bounded to any // environment if ((null == cachedEnvironment) || !ObjectUtils.equals(cachedEnvironment.getId(), environment.getId())) { EnvironmentMappingUpdateJob mappingUpdateJob = mappingUpdateJobFactory.getObject(); mappingUpdateJob.setEnvironment(environment); mappingUpdateJob.setAgentCacheEntry(agentCacheEntry); executor.execute(mappingUpdateJob); } } catch (BusinessException e) { // if we have exception by resolving new environment run job with no new // environment EnvironmentMappingUpdateJob mappingUpdateJob = mappingUpdateJobFactory.getObject(); mappingUpdateJob.setAgentCacheEntry(agentCacheEntry); executor.execute(mappingUpdateJob); } } }
public static void main(String args[]) { Semaphore s = new Semaphore(5); NumberServer n = new NumberServer(); ExecutorService executorService = Executors.newFixedThreadPool(10); List<Integer> list = Collections.synchronizedList(new ArrayList<Integer>()); for (int i = 0; i < 10; i++) { try { s.acquire(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } executorService.execute( new Runnable() { public void run() { list.add(n.getNumber()); s.release(); } }); } executorService.shutdown(); try { s.acquire(5); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } for (int i = 0; i < 10; i++) { System.out.println(list.get(i)); } }
public static void main(String[] args) throws InterruptedException, ExecutionException, TimeoutException { ExecutorService executor = Executors.newFixedThreadPool(5); CompletableFuture<String> task1 = CompletableFuture.supplyAsync( () -> { try { System.out.println(Thread.currentThread().getName() + ": firstTask"); TimeUnit.SECONDS.sleep(2); } catch (Exception e) { } return "1"; }); CompletableFuture<String> task2 = CompletableFuture.supplyAsync( () -> { try { System.out.println(Thread.currentThread().getName() + ": secondTask"); TimeUnit.SECONDS.sleep(3); } catch (Exception e) { } return "2"; }); // a new thread from the supplied executor will execute third task task1.acceptEitherAsync( task2, (x) -> { System.out.println(Thread.currentThread().getName() + ": thirdTask " + x); }, executor); TimeUnit.SECONDS.sleep(5); System.out.println(Thread.currentThread().getName() + ": " + task1.get()); executor.shutdown(); }
@Override public synchronized void close() throws SQLException { try { closeLock.writeLock().lock(); if (closed) { return; } closed = true; } finally { closeLock.writeLock().unlock(); } try { Collection<ConnectionQueryServices> connectionQueryServices = connectionQueryServicesMap.values(); try { SQLCloseables.closeAll(connectionQueryServices); } finally { connectionQueryServices.clear(); } } finally { if (services != null) { try { services.close(); } finally { ExecutorService executor = services.getExecutor(); // Even if something wrong happened while closing services above, we still // want to set it to null. Otherwise, we will end up having a possibly non-working // services instance. services = null; executor.shutdown(); } } } }
public void engage() { System.out.println("Starting the Disruptor"); // starts the event processors final RingBuffer<DemoEvent> ringBuf = disruptor.start(); // now we start the publishers List<Future<?>> futures = new ArrayList<Future<?>>(); ExecutorService execService = Executors.newFixedThreadPool(NUM_PUBLISHERS); try { for (DisruptorDemoPublisher pub : pubs) { futures.add(execService.submit(pub)); } // wait for each publisher to finish for (Future<?> f : futures) { f.get(); } // this should wait until all events are processed disruptor.shutdown(); } catch (Exception e) { System.out.println(e.toString()); e.printStackTrace(); } finally { execService.shutdown(); } }
public final void a() { try { cm.a() .b() .a( "Crashlytics", (new StringBuilder("Executing shutdown hook for ")).append(a).toString()); b.shutdown(); if (!b.awaitTermination(c, d)) { cm.a() .b() .a( "Crashlytics", (new StringBuilder()) .append(a) .append( " did not shut down in the allocated time. Requesting immediate shutdown.") .toString()); b.shutdownNow(); } return; } catch (InterruptedException interruptedexception) { cm.a() .b() .a( "Crashlytics", String.format( Locale.US, "Interrupted while waiting for %s to shut down. Requesting immediate shutdown.", new Object[] {a})); } b.shutdownNow(); }
private void multicastJoin(int count, final boolean sleep) throws InterruptedException { final TestHazelcastInstanceFactory nodeFactory = createHazelcastInstanceFactory(count); final Config config = new Config(); config.setProperty("hazelcast.wait.seconds.before.join", "5"); config.getNetworkConfig().getJoin().getMulticastConfig().setMulticastTimeoutSeconds(25); final ConcurrentMap<Integer, HazelcastInstance> map = new ConcurrentHashMap<Integer, HazelcastInstance>(); final CountDownLatch latch = new CountDownLatch(count); final ExecutorService ex = Executors.newCachedThreadPool(); for (int i = 0; i < count; i++) { final int index = i; ex.execute( new Runnable() { public void run() { if (sleep) { try { Thread.sleep((int) (1000 * Math.random())); } catch (InterruptedException ignored) { } } HazelcastInstance h = nodeFactory.newHazelcastInstance(config); map.put(index, h); latch.countDown(); } }); } assertOpenEventually(latch); for (HazelcastInstance h : map.values()) { assertEquals(count, h.getCluster().getMembers().size()); } ex.shutdown(); }
public void actionPerformed(ActionEvent e) { for (int i = 0; i < 4; i++) { if (e.getSource() == Answer[i]) { // Answer[i].setSelected(false); Charge(i); } } if (e.getSource() == Sure) { ChargeSpell(); } else if (e.getSource() == Speak) { ExecutorService executorService = Executors.newFixedThreadPool(1); executorService.execute( new Runnable() { public void run() { try { Speak.setEnabled(false); WordTextFile.setText(sound.startRec()); Speak.setEnabled(true); System.out.println("done"); } catch (Exception e2) { } } }); // Dictionary.takeWordSearch.startSearch(); executorService.shutdown(); } }
public void unInit() { if (mExecutorService != null) { mExecutorService.shutdown(); mExecutorService.shutdownNow(); mExecutorService = null; } }
@Override public synchronized void connect( String url, RunnableExchangeEventListener runnableExchangeEventListener) { log.info("Connecting..."); // Validate inputs Assert.notNull(runnableExchangeEventListener, "runnableMarketDataListener cannot be null"); // Validate state if (eventExecutorService.isShutdown()) { throw new IllegalStateException( "Service has been stopped. Create a new one rather than reuse a reference."); } try { log.debug( "Attempting to open a socketIO against {}:{}", url, exchangeSpecification.getPort()); this.runnableExchangeEventProducer = new RunnableSocketIOEventProducer(producerEventQueue); this.socketIO = new SocketIO(url, (RunnableSocketIOEventProducer) runnableExchangeEventProducer); } catch (IOException e) { throw new ExchangeException("Failed to open socket!", e); } // Configure the exchange event listener event queue runnableExchangeEventListener.setExchangeEventQueue(producerEventQueue); // Submit the event threads to their services eventExecutorService.submit(runnableExchangeEventProducer); eventExecutorService.submit(runnableExchangeEventListener); log.info("Socket connected OK. Check queues for events."); }
public void close() { if (shuttingDown.getAndSet(true)) { return; } mutex.lock(); try { while (backgroundCompaction != null) { backgroundCondition.awaitUninterruptibly(); } } finally { mutex.unlock(); } compactionExecutor.shutdown(); try { compactionExecutor.awaitTermination(1, TimeUnit.DAYS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } try { versions.destroy(); } catch (IOException ignored) { } try { log.close(); } catch (IOException ignored) { } tableCache.close(); dbLock.release(); }