Tabnine Logo
ExecutorService.submit
Code IndexAdd Tabnine to your IDE (free)

How to use
submit
method
in
java.util.concurrent.ExecutorService

Best Java code snippets using java.util.concurrent.ExecutorService.submit (Showing top 20 results out of 37,737)

Refine searchRefine arrow

  • Future.get
  • Executors.newFixedThreadPool
  • ExecutorService.shutdown
  • AmazonEC2AsyncClient.beforeClientExecution
  • List.add
  • ExecutorService.awaitTermination
  • Test.<init>
origin: aws/aws-sdk-java

public void submit(GeneratorTask task) {
  if (DEBUG) {
    try {
      task.call();
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  } else {
    futures.add(executor.submit(task));
  }
}
origin: neo4j/neo4j

  @Deprecated
  public static <T> Future<T> future( final Callable<T> task )
  {
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<T> future = executor.submit( task );
    executor.shutdown();
    return future;
  }
}
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: apache/hive

@Test
public void testGetInputPathsPool() throws IOException, ExecutionException, InterruptedException {
 List<Path> pathsToAdd = new ArrayList<>();
 Path path = new Path("dummy-path");
 pathsToAdd.add(path);
 pathsToAdd.add(path);
 pathsToAdd.add(path);
 ExecutorService pool = mock(ExecutorService.class);
 Future mockFuture = mock(Future.class);
 when(mockFuture.get()).thenReturn(path);
 when(pool.submit(any(Callable.class))).thenReturn(mockFuture);
 Utilities.getInputPathsWithPool(mock(JobConf.class), mock(MapWork.class), mock(Path.class), mock(Context.class),
     false, pathsToAdd, pool);
 verify(pool, times(3)).submit(any(Callable.class));
 verify(pool).shutdown();
 verify(pool).shutdownNow();
}
origin: apache/kafka

@Test
public void testCheckQuotasInMultiThreads() throws InterruptedException, ExecutionException {
  final Metrics metrics = new Metrics(new MetricConfig().quota(Quota.upperBound(Double.MAX_VALUE))
  final int threadCount = 10;
  final CountDownLatch latch = new CountDownLatch(1);
  ExecutorService service = Executors.newFixedThreadPool(threadCount);
  List<Future<Throwable>> workers = new ArrayList<>(threadCount);
  boolean needShutdown = true;
    for (int i = 0; i != threadCount; ++i) {
      final int index = i;
      workers.add(service.submit(new Callable<Throwable>() {
        @Override
        public Throwable call() {
    service.shutdown();
    assertTrue(service.awaitTermination(10, TimeUnit.SECONDS));
    needShutdown = false;
    for (Future<Throwable> callable : workers) {
      assertTrue("If this failure happen frequently, we can try to increase the wait time", callable.isDone());
      assertNull("Sensor#checkQuotas SHOULD be thread-safe!", callable.get());
origin: apache/hive

private void llapCachePurge(final SessionState ss, final LlapRegistryService llapRegistryService) throws Exception {
 ExecutorService executorService = Executors.newCachedThreadPool();
 List<Future<Long>> futures = new ArrayList<>();
 Collection<LlapServiceInstance> instances = llapRegistryService.getInstances().getAll();
 for (LlapServiceInstance instance : instances) {
  futures.add(executorService.submit(new PurgeCallable(ss.getConf(), instance)));
 }
 int i = 0;
 for (LlapServiceInstance instance : instances) {
  Future<Long> future = futures.get(i);
  ss.out.println(Joiner.on("\t").join(instance.getHost(), future.get()));
  i++;
 }
}
origin: apache/incubator-druid

@Test
public void testLoadSegment() throws ExecutionException, InterruptedException, SegmentLoadingException
{
 final List<Future<Boolean>> futures = segments.stream()
                        .map(
                          segment -> executor.submit(
                            () -> segmentManager.loadSegment(segment)
                          )
                        )
                        .collect(Collectors.toList());
 for (Future<Boolean> eachFuture : futures) {
  Assert.assertTrue(eachFuture.get());
 }
 assertResult(segments);
}
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: ReactiveX/RxJava

@Test(timeout = 5000)
public void fromFutureNormal() {
  ExecutorService exec = Executors.newSingleThreadExecutor();
  try {
    Completable c = Completable.fromFuture(exec.submit(new Runnable() {
      @Override
      public void run() {
        // no action
      }
    }));
    c.blockingAwait();
  } finally {
    exec.shutdown();
  }
}
origin: google/guava

@Override
public void runWithTimeout(Runnable runnable, long timeoutDuration, TimeUnit timeoutUnit)
  throws TimeoutException, InterruptedException {
 checkNotNull(runnable);
 checkNotNull(timeoutUnit);
 checkPositiveTimeout(timeoutDuration);
 Future<?> future = executor.submit(runnable);
 try {
  future.get(timeoutDuration, timeoutUnit);
 } catch (InterruptedException | TimeoutException e) {
  future.cancel(true /* mayInterruptIfRunning */);
  throw e;
 } catch (ExecutionException e) {
  wrapAndThrowRuntimeExecutionExceptionOrError(e.getCause());
  throw new AssertionError();
 }
}
origin: google/j2objc

  public void run() {
    ExecutorService service = Executors.newSingleThreadExecutor();
    Callable<Object> callable = new Callable<Object>() {
      public Object call() throws Exception {
        runTestMethod();
        return null;
      }
    };
    Future<Object> result = service.submit(callable);
    service.shutdown();
    try {
      boolean terminated = service.awaitTermination(timeout,
          TimeUnit.MILLISECONDS);
      if (!terminated) {
        service.shutdownNow();
      }
      result.get(0, TimeUnit.MILLISECONDS); // throws the exception if one occurred during the invocation
    } catch (TimeoutException e) {
      addFailure(new Exception(String.format("test timed out after %d milliseconds", timeout)));
    } catch (Exception e) {
      addFailure(e);
    }
  }
});
origin: stackoverflow.com

 ExecutorService exec = Executors.newFixedThreadPool(SOME_NUM_OF_THREADS);
try {
  for (final Object o : list) {
    exec.submit(new Runnable() {
      @Override
      public void run() {
        // do stuff with o.
      }
    });
  }
} finally {
  exec.shutdown();
}
origin: aws/aws-sdk-java

@Override
public java.util.concurrent.Future<AcceptVpcEndpointConnectionsResult> acceptVpcEndpointConnectionsAsync(final AcceptVpcEndpointConnectionsRequest request,
    final com.amazonaws.handlers.AsyncHandler<AcceptVpcEndpointConnectionsRequest, AcceptVpcEndpointConnectionsResult> asyncHandler) {
  final AcceptVpcEndpointConnectionsRequest finalRequest = beforeClientExecution(request);
  return executorService.submit(new java.util.concurrent.Callable<AcceptVpcEndpointConnectionsResult>() {
    @Override
    public AcceptVpcEndpointConnectionsResult call() throws Exception {
      AcceptVpcEndpointConnectionsResult result = null;
      try {
        result = executeAcceptVpcEndpointConnections(finalRequest);
      } catch (Exception ex) {
        if (asyncHandler != null) {
          asyncHandler.onError(ex);
        }
        throw ex;
      }
      if (asyncHandler != null) {
        asyncHandler.onSuccess(finalRequest, result);
      }
      return result;
    }
  });
}
origin: stackoverflow.com

 ExecutorService service = Executors.newSingleThreadExecutor();

try {
  Runnable r = new Runnable() {
    @Override
    public void run() {
      // Database task
    }
  };

  Future<?> f = service.submit(r);

  f.get(2, TimeUnit.MINUTES);     // attempt the task for two minutes
}
catch (final InterruptedException e) {
  // The thread was interrupted during sleep, wait or join
}
catch (final TimeoutException e) {
  // Took too long!
}
catch (final ExecutionException e) {
  // An exception from within the Runnable task
}
finally {
  service.shutdown();
}
origin: stackoverflow.com

 class TaskAsCallable implements Callable<Result> {
  @Override
  public Result call() {
    return a new Result() // this is where the work is done.
  }
}

ExecutorService executor = Executors.newFixedThreadPool(300);
Future<Result> task = executor.submit(new TaskAsCallable());
Result result = task.get(); // this blocks until result is ready
origin: robolectric/robolectric

private static void shutdownDbExecutor(ExecutorService executorService, Collection<SQLiteConnection> connections) {
 for (final SQLiteConnection connection : connections) {
  getFuture("close connection on reset", executorService.submit(new Callable<Void>() {
   @Override
   public Void call() throws Exception {
    connection.dispose();
    return null;
   }
  }));
 }
 executorService.shutdown();
 try {
  executorService.awaitTermination(30, TimeUnit.SECONDS);
 } catch (InterruptedException e) {
  throw new RuntimeException(e);
 }
}
origin: google/guava

 /** Runs {@code callable} concurrently {@code numberOfThreads} times. */
 @GwtIncompatible // concurrency
 private void runConcurrentTest(int numberOfThreads, final Callable<Void> callable)
   throws Exception {
  ExecutorService executorService = Executors.newFixedThreadPool(numberOfThreads);
  final CountDownLatch startLatch = new CountDownLatch(numberOfThreads);
  final CountDownLatch doneLatch = new CountDownLatch(numberOfThreads);
  for (int i = numberOfThreads; i > 0; i--) {
   @SuppressWarnings("unused") // go/futurereturn-lsc
   Future<?> possiblyIgnoredError =
     executorService.submit(
       new Callable<Void>() {
        @Override
        public Void call() throws Exception {
         startLatch.countDown();
         startLatch.await();
         callable.call();
         doneLatch.countDown();
         return null;
        }
       });
  }
  doneLatch.await();
 }
}
origin: stackoverflow.com

 ExecutorService pool = Executors.newFixedThreadPool(10);
for (String name : fileNames) {
  pool.submit(new DownloadTask(name, toPath));
}
pool.shutdown();
pool.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
// all tasks have now finished (unless an exception is thrown above)
origin: TooTallNate/Java-WebSocket

protected void consumeDelegatedTasks() {
  Runnable task;
  while ( ( task = sslEngine.getDelegatedTask() ) != null ) {
    tasks.add( exec.submit( task ) );
    // task.run();
  }
}
origin: neo4j/neo4j

private void applyBatchesInParallel( ParallelNativeIndexPopulator<GenericKey,NativeIndexValue> populator, int batchCountPerThread )
    throws ExecutionException, InterruptedException
{
  CountDownLatch startSignal = new CountDownLatch( THREADS );
  List<Future<Void>> futures = new ArrayList<>();
  for ( int i = 0; i < THREADS; i++ )
  {
    futures.add( executorService.submit( () ->
    {
      // Wait for all to get into pole position, this is because we want to make sure all threads are used.
      startSignal.countDown();
      startSignal.await();
      for ( int j = 0; j < batchCountPerThread; j++ )
      {
        populator.add( asList( update( next.getAndIncrement() ) ) );
      }
      return null;
    } ) );
  }
  for ( Future<Void> future : futures )
  {
    future.get();
  }
}
java.util.concurrentExecutorServicesubmit

Javadoc

Submits a Runnable task for execution and returns a Future representing that task. The Future's get method will return null upon successful completion.

Popular methods of ExecutorService

  • shutdown
    Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks wil
  • execute
  • shutdownNow
    Attempts to stop all actively executing tasks, halts the processing of waiting tasks, and returns a
  • awaitTermination
    Blocks until all tasks have completed execution after a shutdown request, or the timeout occurs, or
  • isShutdown
    Returns true if this executor has been shut down.
  • isTerminated
    Returns true if all tasks have completed following shut down. Note that isTerminated is never true
  • invokeAll
    Executes the given tasks, returning a list of Futures holding their status and results when all comp
  • invokeAny
    Executes the given tasks, returning the result of one that has completed successfully (i.e., without
  • <init>

Popular in Java

  • Updating database using SQL prepared statement
  • requestLocationUpdates (LocationManager)
  • scheduleAtFixedRate (Timer)
  • getResourceAsStream (ClassLoader)
  • GridLayout (java.awt)
    The GridLayout class is a layout manager that lays out a container's components in a rectangular gri
  • Menu (java.awt)
  • Charset (java.nio.charset)
    A charset is a named mapping between Unicode characters and byte sequences. Every Charset can decode
  • Scanner (java.util)
    A parser that parses a text string of primitive types and strings with the help of regular expressio
  • JButton (javax.swing)
  • JLabel (javax.swing)
  • 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