private int[] computeClusterNumberRange(int min, int max, int hop, Optimizer optimizer) throws ExecutionException, InterruptedException { ExecutorService executor; if (parallelWorkers > 1) { executor = Executors.newFixedThreadPool(2); } else { executor = Executors.newFixedThreadPool(1); } ConcurrentMap<Integer, Double> sharedScores = new ConcurrentHashMap<>(); SearchSpaceBoundaryFinder_old upperBoundFinder = new SearchSpaceUpperBoundaryFinder_old(min, max, hop, optimizer, sharedScores); Future<Integer> futureUpperBound = executor.submit(upperBoundFinder); SearchSpaceBoundaryFinder_old lowerBoundFinder = new SearchSpaceLowerBoundaryFinder_old(min, max, hop, optimizer, sharedScores); Future<Integer> futureLowerBound = executor.submit(lowerBoundFinder); executor.shutdown(); int[] r = new int[2]; Integer realMin = futureLowerBound.get(); Integer realMax = futureUpperBound.get(); r[0] = realMin == null ? -1 : realMin; r[1] = realMax == null ? -1 : realMax; scores.putAll(lowerBoundFinder.getSplitsAndScores()); // scores.putAll(upperBoundFinder.getSplitsAndScores()); return r; }
public Future<Integer> execute() { if (executor == null) executor = Executors.newFixedThreadPool(4); if (executor.isShutdown()) executor = Executors.newFixedThreadPool(4); ExecutorCompletionService<Integer> commandExecutor = new ExecutorCompletionService<Integer>(executor); return commandExecutor.submit(this); }
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; }
@Override public BxDocument segmentDocument(BxDocument document) throws AnalysisException { Map<BxPage, List<Component>> componentMap = new HashMap<BxPage, List<Component>>(); ExecutorService exec = Executors.newFixedThreadPool(PdfNLMContentExtractor.THREADS_NUMBER); ArrayList<Callable<NumBxPage>> tasks = new ArrayList<Callable<NumBxPage>>(); for (BxPage page : document.getPages()) { tasks.add(new ComponentCounter(page)); } List<Future<NumBxPage>> results; try { results = exec.invokeAll(tasks); exec.shutdown(); for (Future<NumBxPage> result : results) { NumBxPage p = result.get(); componentMap.put(p.page, p.components); } } catch (ExecutionException ex) { throw new AnalysisException("Cannot segment pages!", ex); } catch (InterruptedException ex) { throw new AnalysisException("Cannot segment pages!", ex); } this.computeDocumentOrientation(componentMap); BxDocument output = new BxDocument(); BxPage[] pages = new BxPage[document.getPages().size()]; exec = Executors.newFixedThreadPool(PdfNLMContentExtractor.THREADS_NUMBER); tasks = new ArrayList<Callable<NumBxPage>>(); int i = 0; for (BxPage page : document.getPages()) { tasks.add(new SingleSegmenter(page, i++)); } try { results = exec.invokeAll(tasks); exec.shutdown(); for (Future<NumBxPage> result : results) { NumBxPage p = result.get(); pages[p.index] = p.page; } for (BxPage p : pages) { if (p.getBounds() != null) { output.addPage(p); } } return output; } catch (ExecutionException ex) { throw new AnalysisException("Cannot segment pages!", ex); } catch (InterruptedException ex) { throw new AnalysisException("Cannot segment pages!", ex); } }
@Before public void setUp() throws Exception { writer = (ThreadPoolExecutor) Executors.newFixedThreadPool(writerThreadSize); reader = (ThreadPoolExecutor) Executors.newFixedThreadPool(readThreadSize); writer.prestartAllCoreThreads(); reader.prestartAllCoreThreads(); for (int i = 0; i < testMapSize; i++) { map.put(i, new AtomicReferenceTest()); } }
public final void init(final File dir) { this.dbExecutor.shutdownNow(); this.qExecutor.shutdown(); this.dbExecutor = Executors.newFixedThreadPool(3); this.qExecutor = Executors.newFixedThreadPool(2); System.runFinalization(); System.gc(); }
public void setNbThread(String s) { if (s != null && s.matches("\\d+")) { option.setNbThreads(Integer.parseInt(s)); final int nbThreads = option.getNbThreads(); this.executorService = Executors.newFixedThreadPool(nbThreads); } if ("auto".equalsIgnoreCase(s)) { option.setNbThreads(Option.defaultNbThreads()); final int nbThreads = option.getNbThreads(); this.executorService = Executors.newFixedThreadPool(nbThreads); } }
public PodcastService() { ThreadFactory threadFactory = new ThreadFactory() { public Thread newThread(Runnable r) { Thread t = Executors.defaultThreadFactory().newThread(r); t.setDaemon(true); return t; } }; refreshExecutor = Executors.newFixedThreadPool(5, threadFactory); downloadExecutor = Executors.newFixedThreadPool(3, threadFactory); scheduledExecutor = Executors.newSingleThreadScheduledExecutor(threadFactory); }
public static void start() { RibbonApp ribbonApp = new RibbonApp(); ServerBootstrap bootstrap = new ServerBootstrap( new NioServerSocketChannelFactory( Executors.newFixedThreadPool(CORES), Executors.newFixedThreadPool(CORES))); bootstrap.setPipelineFactory(newHttpPipelineFactory(ribbonApp)); bootstrap.bind(new InetSocketAddress(PORT)); System.out.println("Ribbon server started on port: " + PORT); }
public AbstractActionOperationsDelegate( String delegateId, TriggerOperator triggerOperator, DaoConfigurer daoConfigurer, Executor executor, int threadPoolSize) { this.triggerOperator = triggerOperator; this.actionInstanceDao = daoConfigurer.getActionInstanceDao(); this.executionDao = daoConfigurer.getExecutionDao(); this.executor = executor; this.delegateId = delegateId; this.executeService = Executors.newFixedThreadPool(threadPoolSize); this.cancelService = Executors.newFixedThreadPool(threadPoolSize > 1 ? threadPoolSize / 2 : threadPoolSize); }
@SuppressWarnings("unchecked") public static void createDisruptors(DisruptorType type, DisruptorConfig disruptorConfig) { WaitStrategy inboundWaitStrategy = getWaitStrategy(disruptorConfig.getDisruptorWaitStrategy()); for (int i = 0; i < disruptorConfig.getNoDisruptors(); i++) { ExecutorService executorService = Executors.newFixedThreadPool(disruptorConfig.getNoOfEventHandlersPerDisruptor()); Disruptor disruptor = new Disruptor<>( CarbonDisruptorEvent.EVENT_FACTORY, disruptorConfig.getBufferSize(), executorService, ProducerType.MULTI, inboundWaitStrategy); ExceptionHandler exh = new GenericExceptionHandler(); EventHandler[] eventHandlers = new EventHandler[disruptorConfig.getNoOfEventHandlersPerDisruptor()]; for (int j = 0; j < disruptorConfig.getNoOfEventHandlersPerDisruptor(); j++) { EventHandler eventHandler = new CarbonDisruptorEventHandler(); eventHandlers[j] = eventHandler; } disruptor.handleEventsWith(eventHandlers); for (EventHandler eventHandler : eventHandlers) { disruptor.handleExceptionsFor(eventHandler).with(exh); } disruptorConfig.addDisruptor(disruptor.start()); } disruptorConfigHashMap.put(type, disruptorConfig); }
public void collectQueueStats( final Azure azure, final String namespaceName, Set<String> queueNames, Set<String> queueStats, int queueThreads) throws TaskExecutionException { final Map<String, String> valueMap = createValueMap(azure, namespaceName, QUEUES, queueStats); ListeningExecutorService queueService = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(queueThreads)); final CountDownLatch countDownLatch = new CountDownLatch(queueNames.size()); try { for (final String queueName : queueNames) { try { ListenableFuture getQueueNames = queueService.submit( new Runnable() { public void run() { getStatsFromAzure(azure, namespaceName, valueMap, queueName, QUEUES); } }); Futures.addCallback( getQueueNames, new FutureCallback<Void>() { public void onSuccess(Void nothing) { countDownLatch.countDown(); } public void onFailure(Throwable thrown) { countDownLatch.countDown(); logger.error( "Unable to get stats for queue [" + queueName + "] in namespace [" + namespaceName + "]", thrown); } }); } catch (Exception e) { logger.error( "Error getting stats for queue [" + namespaceName + "/" + queueName + "]", e); throw new TaskExecutionException( "Error getting stats for queue [" + namespaceName + "/" + queueName + "]", e); } } } finally { queueService.shutdown(); } try { countDownLatch.await(); } catch (InterruptedException e) { logger.error("Unable to wait till getting the queue stats", e); } }
public static void main(String... args) { final ReactiveCouchbaseDriver driver = ReactiveCouchbaseDriver.apply(); final CouchbaseBucket bucket = new CouchbaseBucket(driver.bucket("default")); final ExecutorService ec = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors()); JsObject document = Json.obj( $("name", "John"), $("surname", "Doe"), $("age", 42), $( "address", Json.obj($("number", 42), $("street", "Baker Street"), $("city", "London")))); bucket .set("john-doe", document, FormatHelper.JS_OBJECT_FORMAT) .onSuccess( new Functionnal.Action<OperationStatus>() { @Override public void call(OperationStatus operationStatus) { System.out.println("Operation status : " + operationStatus.getMessage()); driver.shutdown(); } }, ec); }
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(); } }
@Test public void incrementConcurrent() throws Exception { Collection<Callable<Boolean>> tasks = new ArrayList<Callable<Boolean>>(); for (int i = 0; i < 100; i++) { tasks.add( new Callable<Boolean>() { @Override public Boolean call() throws Exception { InMemoryMetricRepositoryTests.this.repository.increment("foo", 1, new Date()); return true; } }); tasks.add( new Callable<Boolean>() { @Override public Boolean call() throws Exception { InMemoryMetricRepositoryTests.this.repository.increment("foo", -1, new Date()); return true; } }); } List<Future<Boolean>> all = Executors.newFixedThreadPool(10).invokeAll(tasks); for (Future<Boolean> future : all) { assertTrue(future.get(1, TimeUnit.SECONDS)); } assertEquals(0, this.repository.findOne("foo").getValue(), 0.01); }
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; }
public EventExecutorImpl(int threadPoolSize) { /** 初始化执行器线程池 */ System.out.println("init Thread Pool for " + threadPoolSize); this.executorService = Executors.newFixedThreadPool( threadPoolSize, new EventThreadFactory(EventUtil.EventContainerName)); }
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) { 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; } }); } }
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()); }
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 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 filterActionPerformed(java.awt.event.ActionEvent evt) { // FilterFunctions.chromPosition(); FilterFunctions rf2 = new FilterFunctions("ValidValuesFilter"); ShowTable.threadExecutor = Executors.newFixedThreadPool(1); ShowTable.threadExecutor.execute(rf2); ShowTable.threadExecutor.shutdown(); }
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); }
@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; } }
@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 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 String test(FailureDetector failureDetector) throws Exception { Node node = Iterables.get(failureDetectorConfig.getCluster().getNodes(), 0); CountDownLatch countDownLatch = new CountDownLatch(1); Listener listener = new Listener(failureDetectorConfig.getTime()); failureDetector.addFailureDetectorListener(listener); ExecutorService threadPool = Executors.newFixedThreadPool(11); for (int i = 0; i < 10; i++) threadPool.submit( new NodeAccessorRunnable(failureDetector, node, countDownLatch, null, null, null, 0, 10)); threadPool.submit(new TimedUnavailability(failureDetector, node, countDownLatch)); threadPool.shutdown(); // If we get stuck, we should give the user the opportunity to get a // thread dump. if (!threadPool.awaitTermination(60, TimeUnit.SECONDS)) { System.out.println("Threads appear to be stuck"); threadPool.awaitTermination(Long.MAX_VALUE, TimeUnit.SECONDS); } return Class.forName(failureDetectorConfig.getImplementationClassName()).getSimpleName() + ", " + listener.getDelta(); }
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(); } }