/** * Initializes suggester data for specified indexes. The data is initialized asynchronously. * @param luceneIndexes paths to Lucene indexes and name with which the index should be associated */ public void init(final Collection<NamedIndexDir> luceneIndexes) { if (luceneIndexes == null || luceneIndexes.isEmpty()) { logger.log(Level.INFO, "No index directories found, exiting..."); return; } if (!projectsEnabled && luceneIndexes.size() > 1) { throw new IllegalArgumentException("Projects are not enabled and multiple Lucene indexes were passed"); } synchronized (lock) { logger.log(Level.INFO, "Initializing suggester"); ExecutorService executor = Executors.newWorkStealingPool(rebuildParallelismLevel); for (NamedIndexDir indexDir : luceneIndexes) { submitInitIfIndexExists(executor, indexDir); } shutdownAndAwaitTermination(executor, "Suggester successfully initialized"); } }
/** * Rebuilds the data structures for specified indexes. * @param indexDirs paths to lucene indexes and name with which the index should be associated */ public void rebuild(final Collection<NamedIndexDir> indexDirs) { if (indexDirs == null || indexDirs.isEmpty()) { logger.log(Level.INFO, "Not rebuilding suggester data because no index directories were specified"); return; } synchronized (lock) { logger.log(Level.INFO, "Rebuilding the following suggesters: {0}", indexDirs); ExecutorService executor = Executors.newWorkStealingPool(rebuildParallelismLevel); for (NamedIndexDir indexDir : indexDirs) { SuggesterProjectData data = this.projectData.get(indexDir.name); if (data != null) { executor.submit(getRebuildRunnable(data)); } else { submitInitIfIndexExists(executor, indexDir); } } shutdownAndAwaitTermination(executor, "Suggesters for " + indexDirs + " were successfully rebuilt"); } }
@Test public void forkJoinPoolWorkQueueRejectsSubscribers() { ExecutorService executorService = Executors.newWorkStealingPool(2); WorkQueueProcessor<String> bc = WorkQueueProcessor.<String>builder().executor(executorService).bufferSize(16).build(); CountDownLatch latch = new CountDownLatch(2); TestWorkQueueSubscriber spec1 = new TestWorkQueueSubscriber(latch, "spec1"); TestWorkQueueSubscriber spec2 = new TestWorkQueueSubscriber(latch, "spec2"); TestWorkQueueSubscriber spec3 = new TestWorkQueueSubscriber(latch, "spec3"); bc.subscribe(spec1); bc.subscribe(spec2); bc.subscribe(spec3); bc.onNext("foo"); bc.onComplete(); assertThat(spec1.error, is(nullValue())); assertThat(spec2.error, is(nullValue())); assertThat(spec3.error, is(notNullValue())); assertThat(spec3.error.getMessage(), is("The executor service could not accommodate another subscriber, detected limit 2")); try { latch.await(1, TimeUnit.SECONDS); } catch (InterruptedException e1) { fail(e1.toString()); } }
ExecutorService executorService4 = Executors.newFixedThreadPool(2); ScheduledExecutorService executorService5 = Executors.newScheduledThreadPool(3); ExecutorService executorService6 = Executors.newWorkStealingPool(4); ExecutorService executorService7 = Executors.unconfigurableExecutorService(executorService4);
private static void test4() throws InterruptedException { ExecutorService executor = Executors.newWorkStealingPool(); List<Callable<String>> callables = Arrays.asList( () -> "task1", () -> "task2", () -> "task3"); executor.invokeAll(callables) .stream() .map(future -> { try { return future.get(); } catch (Exception e) { throw new IllegalStateException(e); } }) .forEach(System.out::println); executor.shutdown(); }
private static void test5() throws InterruptedException, ExecutionException { ExecutorService executor = Executors.newWorkStealingPool(); List<Callable<String>> callables = Arrays.asList( callable("task1", 2), callable("task2", 1), callable("task3", 3)); String result = executor.invokeAny(callables); System.out.println(result); executor.shutdown(); }
final ExecutorService service = Executors.newWorkStealingPool(); for (final Object object : objectsToProcess) { service.submit(() -> { Go to database retrieve data. process save data } }); }
@Configuration @EnableAsync public class AsyncConfig extends AsyncConfigurerSupport { @Override public Executor getAsyncExecutor() { return Executors.newWorkStealingPool(); //will use as many threads as your processor has } }
tp = Executors.newWorkStealingPool(512); // to create up to 512 Threads //todo the creation loop... future = tp.submit(()->{/* your port scan */ return true;}); //todo the waiting processing future.cancel(true);// to cancel your future
public GraphQLQueryExecutor forkJoinExecutorService() { this.executorService = Executors.newWorkStealingPool(); return this; }
@Override public void beforeEach(ExtensionContext context) throws Exception { executorService = Executors.newWorkStealingPool(); }
@Override public void start() { if (_workers == -1) { this.service = Executors.newCachedThreadPool(); } else { this.service = Executors.newWorkStealingPool(_workers); } }
@Bean public ExecutorService executorService(){ return Executors.newWorkStealingPool(); } }
List<String> hosts = fetchTargetHosts(); int concurrentHosts = 10; int concurrentConnections = 100; ExecutorService hostEs=Executors.newWorkStealingPool(concurrentHosts); ExecutorService connEs=Executors.newWorkStealingPool(concurrentConnections); FileDownloader fileDownloader = new FileDownloader(); for(String host: hosts) hostEs.execute(()-> { for(String target: new HostDownloader(host).get()) connEs.execute(()->fileDownloader.accept(target)); });
public static void main(String[] args) throws InterruptedException { System.out.println( Runtime.getRuntime().availableProcessors()); ExecutorService exec = Executors.newWorkStealingPool(); IntStream.range(0, 10) .mapToObj(n -> new ShowThread()) .forEach(exec::execute); exec.awaitTermination(1, TimeUnit.SECONDS); } }
/** * Constructor initializes the fields. */ public SearchStreamGang(List<String> phrasesToFind, List<List<CharSequence>> listOfListOfInputToSearch) { // Store the phrases to search for. mPhrasesToFind = phrasesToFind; // Create an Iterator for the array of Strings to search. mInputIterator = listOfListOfInputToSearch.iterator(); // Initialize the Executor with a ForkJoinPool. setExecutor(Executors.newWorkStealingPool()); }
int processorCount = Runtime.getRuntime().availableProcessors(); ExecutorService executor1 = Executors.newFixedThreadPool(processorCount); ExecutorService executor2 = Executors.newScheduledThreadPool(processorCount); ExecutorService executor3 = Executors.newWorkStealingPool(); // java-8 API ForkJoinPool forkJoinPool = new ForkJoinPool(processorCount);
private static void test5() throws InterruptedException, ExecutionException { ExecutorService executor = Executors.newWorkStealingPool(); List<Callable<String>> callables = Arrays.asList( callable("task1", 2), callable("task2", 1), callable("task3", 3)); String result = executor.invokeAny(callables); System.out.println(result); executor.shutdown(); }
public void start(InetSocketAddress address) throws IOException { server = HttpServer.create(address, Integer.parseInt(System.getProperty(BACKLOG, "10"))); // threadpool! server.setExecutor(Executors.newWorkStealingPool(Integer.parseInt(System.getProperty(HTTPPOOL, "10")))); // Only two handlers. server.createContext("/", new MsgHandler()); server.createContext("/status", new StatusHandler()); logger.info("Server started on {} ", server.getAddress()); // Starts in separate thread. server.start(); }
public void start(InetSocketAddress address) throws IOException { server = HttpServer.create(address, Integer.parseInt(System.getProperty(BACKLOG, "10"))); // threadpool! server.setExecutor(Executors.newWorkStealingPool(Integer.parseInt(System.getProperty(HTTPPOOL, "10")))); // Only two handlers. server.createContext("/", new MsgHandler()); server.createContext("/status", new StatusHandler()); logger.info("Server started on {} ", server.getAddress()); // Starts in separate thread. server.start(); }