Tabnine Logo
Executors.newFixedThreadPool
Code IndexAdd Tabnine to your IDE (free)

How to use
newFixedThreadPool
method
in
java.util.concurrent.Executors

Best Java code snippets using java.util.concurrent.Executors.newFixedThreadPool (Showing top 20 results out of 25,065)

Refine searchRefine arrow

  • ExecutorService.submit
  • ExecutorService.shutdown
  • Future.get
  • ExecutorService.awaitTermination
  • PrintStream.println
  • List.add
origin: square/okhttp

private void parallelDrainQueue(int threadCount) {
 ExecutorService executor = Executors.newFixedThreadPool(threadCount);
 for (int i = 0; i < threadCount; i++) {
  executor.execute(new NamedRunnable("Crawler %s", i) {
   @Override protected void execute() {
    try {
     drainQueue();
    } catch (Exception e) {
     e.printStackTrace();
    }
   }
  });
 }
 executor.shutdown();
}
origin: stackoverflow.com

 final ExecutorService producers = Executors.newFixedThreadPool(100);
final ExecutorService consumers = Executors.newFixedThreadPool(100);
while (/* has more work */) {
 producers.submit(...);
}
producers.shutdown();
producers.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
consumers.shutdown();
consumers.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
origin: stackoverflow.com

 ExecutorService taskExecutor = Executors.newFixedThreadPool(4);
while(...) {
 taskExecutor.execute(new MyTask());
}
taskExecutor.shutdown();
try {
 taskExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
 ...
}
origin: stackoverflow.com

 ExecutorService es = Executors.newFixedThreadPool(2);
List<Callable<Object>> todo = new ArrayList<Callable<Object>>(singleTable.size());

for (DataTable singleTable: uniquePhrases) { 
  todo.add(Executors.callable(new ComputeDTask(singleTable))); 
}

List<Future<Object>> answers = es.invokeAll(todo);
origin: apache/hive

@Test
public void testParallelCompilationWithMultipleQuotasAndClientSessionConcurrency() throws Exception {
 conf.setBoolVar(HIVE_SERVER2_PARALLEL_COMPILATION, true);
 conf.setIntVar(HIVE_SERVER2_PARALLEL_COMPILATION_LIMIT, 2);
 initDriver(conf, 10);
 List<CommandProcessorResponse> responseList = new ArrayList<>();
 List<Callable<List<CommandProcessorResponse>>> callables = new ArrayList<>();
 for (int i = 0; i < 5; i++) {
  callables.add(() -> compileAndRespond(true, 2));
 }
 ExecutorService pool = Executors.newFixedThreadPool(callables.size());
 try {
  List<Future<List<CommandProcessorResponse>>> futures = pool.invokeAll(callables);
  for (Future<List<CommandProcessorResponse>> future : futures) {
   responseList.addAll(future.get());
  }
 } finally {
  pool.shutdown();
 }
 verifyThatWaitingCompileOpsCountIsEqualTo(0);
 verifyThatTimedOutCompileOpsCountIsZero(responseList);
 verifyThatConcurrentCompilationWasIndeed(responseList);
}
origin: apache/ignite

/**
 * Starts streamer.
 *
 * @throws IgniteException If failed.
 */
public void start() {
  A.notNull(getStreamer(), "streamer");
  A.notNull(getIgnite(), "ignite");
  A.notNull(topics, "topics");
  A.notNull(consumerCfg, "kafka consumer config");
  A.ensure(threads > 0, "threads > 0");
  A.ensure(null != getSingleTupleExtractor() || null != getMultipleTupleExtractor(),
    "Extractor must be configured");
  log = getIgnite().log();
  executor = Executors.newFixedThreadPool(threads);
  IntStream.range(0, threads).forEach(i -> consumerTasks.add(new ConsumerTask(consumerCfg)));
  for (ConsumerTask task : consumerTasks)
    executor.submit(task);
}
origin: LeonardoZ/java-concurrency-patterns

private void initialize() {
  System.out.println("=== Initializing Resources ===");
  ExecutorService executor = Executors.newFixedThreadPool(3);
  executor.execute(initResource1);
  executor.execute(initResource2);
  executor.execute(initResource3);
  executor.shutdown();
}
origin: stackoverflow.com

 public static void main(String args[]) throws InterruptedException {
  ExecutorService executor = Executors.newFixedThreadPool(1);
  executor.submit(new Runnable() {

    @Override
    public void run() {
      while (true) {
        if (Thread.currentThread().isInterrupted()) break;
      }
    }
  });

  executor.shutdownNow();
  if (!executor.awaitTermination(100, TimeUnit.MICROSECONDS)) {
    System.out.println("Still waiting...");
    System.exit(0);
  }
  System.out.println("Exiting normally...");
}
origin: stackoverflow.com

ExecutorService pool = Executors.newFixedThreadPool(10);
 List<Callable<String>> tasks = new ArrayList<>();
 tasks.add(new Callable<String>() {
   public String call() throws Exception {
     Thread.sleep((new Random().nextInt(5000)) + 500);
     return "Hello world";
   }
 });
 List<Future<String>> results = pool.invokeAll(tasks);
 for (Future<String> future : results) {
   System.out.println(future.get());
 }
 pool.shutdown();
origin: apache/hive

public ConcurrentTestRunner(Class<?> type) throws InitializationError {
 super(type);
 String numThreadsProp = System.getProperty("test.concurrency.num.threads");
 if (numThreadsProp != null) {
  numThreads = Integer.parseInt(numThreadsProp);
 }
 setScheduler(new ConcurrentScheduler(newFixedThreadPool(numThreads, new ConcurrentTestRunnerThreadFactory())));
 System.err.println(">>> ConcurrenTestRunner initialize with " + numThreads + " threads");
 System.err.flush();
}
origin: stackoverflow.com

 ExecutorService exec = Executors.newFixedThreadPool(50);
final Future f = exec.submit(task1);
exec.submit(new Runnable() {
  @Override
  public void run() {
    f.get();
    // do stuff
  }
});
origin: stackoverflow.com

 // create a thread pool with 10 threads, this can be optimized to your hardware
ExecutorService threadPool = Executors.newFixedThreadPool(10);
// submit your handlers to the thread-pool
for (PCHandler handler : handlersToDo) {
  threadPool.submit(handler);
}
// once we have submitted all jobs to the thread pool, it should be shutdown
threadPool.shutdown();
...
origin: apache/kafka

@Test
public void testTimeoutAndRetryJoinGroupIfNeeded() throws Exception {
  setupCoordinator();
  mockClient.prepareResponse(groupCoordinatorResponse(node, Errors.NONE));
  coordinator.ensureCoordinatorReady(mockTime.timer(0));
  ExecutorService executor = Executors.newFixedThreadPool(1);
  try {
    Timer firstAttemptTimer = mockTime.timer(REQUEST_TIMEOUT_MS);
    Future<Boolean> firstAttempt = executor.submit(() -> coordinator.joinGroupIfNeeded(firstAttemptTimer));
    mockTime.sleep(REQUEST_TIMEOUT_MS);
    assertFalse(firstAttempt.get());
    assertTrue(consumerClient.hasPendingRequests(coordinatorNode));
    mockClient.respond(joinGroupFollowerResponse(1, "memberId", "leaderId", Errors.NONE));
    mockClient.prepareResponse(syncGroupResponse(Errors.NONE));
    Timer secondAttemptTimer = mockTime.timer(REQUEST_TIMEOUT_MS);
    Future<Boolean> secondAttempt = executor.submit(() -> coordinator.joinGroupIfNeeded(secondAttemptTimer));
    assertTrue(secondAttempt.get());
  } finally {
    executor.shutdownNow();
    executor.awaitTermination(1000, TimeUnit.MILLISECONDS);
  }
}
origin: ehcache/ehcache3

@Test
public void testMultithreadedXmlParsing() throws InterruptedException, ExecutionException {
 Callable<Configuration> parserTask = () -> new XmlConfiguration(XmlConfigurationTest.class.getResource("/configs/one-cache.xml"));
 ExecutorService service = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
 try {
  for (Future<Configuration> c : service.invokeAll(nCopies(10, parserTask))) {
   assertThat(c.get(), IsNull.notNullValue());
  }
 } finally {
  service.shutdown();
 }
}
origin: google/data-transfer-project

public void initializeWorkers(int workers) {
 if (workers < 1) {
  errorCallback.accept(new IllegalArgumentException("Invalid number of workers: " + workers));
  return;
 }
 executorService = Executors.newFixedThreadPool(workers);
 for (int i = 0; i < workers; i++) {
  WorkerRunner workerRunner = new WorkerRunner();
  executorService.submit(workerRunner);
 }
}
origin: stackoverflow.com

 final ExecutorService pool = Executors.newFixedThreadPool(2);
final List<? extends Callable<String>> callables = Arrays.asList(
  new SleepingCallable("quick", 500),
  new SleepingCallable("slow", 5000));
try {
 for (final Future<String> future : pool.invokeAll(callables)) {
  System.out.println(future.get());
 }
} catch (ExecutionException | InterruptedException ex) { }
pool.shutdown();
origin: apache/incubator-druid

 private void testWithExecutor(
   final DimFilter filter,
   final List<String> expectedRows
 )
 {
  ListeningExecutorService executor = MoreExecutors.listeningDecorator(Executors.newFixedThreadPool(EXECUTOR_NUM_THREADS));

  List<ListenableFuture<?>> futures = new ArrayList<>();

  for (int i = 0; i < EXECUTOR_NUM_TASKS; i++) {
   Runnable runnable = makeFilterRunner(filter, expectedRows);
   ListenableFuture fut = executor.submit(runnable);
   futures.add(fut);
  }

  try {
   Futures.allAsList(futures).get(60, TimeUnit.SECONDS);
  }
  catch (Exception ex) {
   Assert.fail(ex.getMessage());
  }

  executor.shutdown();
 }
}
origin: stackoverflow.com

 import java.util.concurrent.*;
class ThreadIdTest {

 public static void main(String[] args) {

  final int numThreads = 5;
  ExecutorService exec = Executors.newFixedThreadPool(numThreads);

  for (int i=0; i<10; i++) {
   exec.execute(new Runnable() {
    public void run() {
     long threadId = Thread.currentThread().getId();
     System.out.println("I am thread " + threadId + " of " + numThreads);
    }
   });
  }

  exec.shutdown();
 }
}
origin: stackoverflow.com

 public static void main(String args[]) throws InterruptedException {
  ExecutorService executor = Executors.newFixedThreadPool(1);
  executor.submit(new Runnable() {

    @Override
    public void run() {
      try {
        while (true) {Thread.sleep(10);}
      } catch (InterruptedException e) {
        //ok let's get out of here
      }
    }
  });

  executor.shutdownNow();
  if (!executor.awaitTermination(100, TimeUnit.MICROSECONDS)) {
    System.out.println("Still waiting...");
    System.exit(0);
  }
  System.out.println("Exiting normally...");
}
origin: reactor/reactor-core

  @Test
  public void scanExecutorAttributes() {
    Scheduler fixedThreadPool = Schedulers.fromExecutorService(Executors.newFixedThreadPool(3));

    Long test = Integer.MAX_VALUE + 1L;
    System.out.println(test.intValue() == Integer.MAX_VALUE);

    assertThat(Scannable.from(fixedThreadPool).scan(Scannable.Attr.CAPACITY)).isEqualTo(3);
    assertThat(Scannable.from(fixedThreadPool).scan(Scannable.Attr.BUFFERED)).isZero();
    assertThat(Scannable.from(fixedThreadPool).scan(Scannable.Attr.LARGE_BUFFERED)).isZero();

    fixedThreadPool.schedule(() -> {
      assertThat(Scannable.from(fixedThreadPool).scan(Scannable.Attr.BUFFERED)).isNotZero();
      assertThat(Scannable.from(fixedThreadPool).scan(Scannable.Attr.LARGE_BUFFERED)).isNotZero();
    });
  }
}
java.util.concurrentExecutorsnewFixedThreadPool

Javadoc

Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue. At any point, at most nThreads threads will be active processing tasks. If additional tasks are submitted when all threads are active, they will wait in the queue until a thread is available. If any thread terminates due to a failure during execution prior to shutdown, a new one will take its place if needed to execute subsequent tasks. The threads in the pool will exist until it is explicitly ExecutorService#shutdown.

Popular methods of Executors

  • newSingleThreadExecutor
    Creates an Executor that uses a single worker thread operating off an unbounded queue, and uses the
  • newCachedThreadPool
    Creates a thread pool that creates new threads as needed, but will reuse previously constructed thre
  • newSingleThreadScheduledExecutor
    Creates a single-threaded executor that can schedule commands to run after a given delay, or to exec
  • newScheduledThreadPool
    Creates a thread pool that can schedule commands to run after a given delay, or to execute periodica
  • defaultThreadFactory
    Returns a default thread factory used to create new threads. This factory creates all new threads us
  • callable
    Returns a Callable object that, when called, runs the given privileged exception action and returns
  • unconfigurableScheduledExecutorService
    Returns an object that delegates all defined ScheduledExecutorService methods to the given executor,
  • unconfigurableExecutorService
    Returns an object that delegates all defined ExecutorService methods to the given executor, but not
  • newWorkStealingPool
    Creates a thread pool that maintains enough threads to support the given parallelism level, and may
  • privilegedThreadFactory
    Legacy security code; do not use.
  • privilegedCallable
    Legacy security code; do not use.
  • privilegedCallableUsingCurrentClassLoader
    Legacy security code; do not use.
  • privilegedCallable,
  • privilegedCallableUsingCurrentClassLoader

Popular in Java

  • Updating database using SQL prepared statement
  • putExtra (Intent)
  • compareTo (BigDecimal)
  • getSupportFragmentManager (FragmentActivity)
  • Window (java.awt)
    A Window object is a top-level window with no borders and no menubar. The default layout for a windo
  • URL (java.net)
    A Uniform Resource Locator that identifies the location of an Internet resource as specified by RFC
  • ResultSet (java.sql)
    An interface for an object which represents a database table entry, returned as the result of the qu
  • JFrame (javax.swing)
  • Get (org.apache.hadoop.hbase.client)
    Used to perform Get operations on a single row. To get everything for a row, instantiate a Get objec
  • Runner (org.openjdk.jmh.runner)
  • Top 17 Free Sublime Text Plugins
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimAtomGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyStudentsTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now