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

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

Best Java code snippets using java.util.concurrent.ExecutorService.shutdown (Showing top 20 results out of 30,222)

Refine searchRefine arrow

  • ExecutorService.awaitTermination
  • Executors.newFixedThreadPool
  • ExecutorService.submit
  • Future.get
  • PrintStream.println
  • ExecutorService.shutdownNow
canonical example by Tabnine

public void runThreadTask() {
 ExecutorService service = Executors.newCachedThreadPool();
 service.execute(
   () -> {
    // ... do something inside runnable task
   });
 service.shutdown();
}
origin: iluwatar/java-design-patterns

 /**
  * Stops the pool of workers.
  * 
  * @throws InterruptedException if interrupted while stopping pool of workers.
  */
 @Override
 public void stop() throws InterruptedException {
  executorService.shutdown();
  executorService.awaitTermination(4, TimeUnit.SECONDS);
 }
}
origin: iluwatar/java-design-patterns

/**
 * Stops logging clients. This is a blocking call.
 */
public void stop() {
 service.shutdown();
 if (!service.isTerminated()) {
  service.shutdownNow();
  try {
   service.awaitTermination(1000, TimeUnit.SECONDS);
  } catch (InterruptedException e) {
   LOGGER.error("exception awaiting termination", e);
  }
 }
 LOGGER.info("Logging clients stopped");
}
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: PipelineAI/pipeline

@AfterClass
public static void tearDown() {
  threadPool.shutdown();
  try {
    threadPool.awaitTermination(10, TimeUnit.SECONDS);
  } catch (InterruptedException ie) {
    System.out.println("Thread pool never terminated in HystrixRollingPercentileTest");
  }
}
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: 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: square/okhttp

public void run() throws IOException {
 try {
  long count = getTestCount();
  System.out.println("Test count: " + count);
  for (long number = 1; number <= count; number++) {
   runTest(number, count);
  }
  updateReports();
 } finally {
  client.dispatcher().executorService().shutdown();
 }
}
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

 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

 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: 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: 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: 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: org.testng/testng

 public List<FutureType> submitTasksAndWait(List<? extends Callable<FutureType>> tasks) {

  List<Future<FutureType>> takes = Lists.newArrayList(tasks.size());
  for (Callable<FutureType> callable : tasks) {
   takes.add(m_completionService.submit(callable));
  }

  List<FutureType> result = Lists.newArrayList(takes.size());
  for (Future<FutureType> take : takes) {
   try {
    result.add(take.get());
   } catch (InterruptedException | ExecutionException e) {
    throw new TestNGException(e);
   }
  }

  m_executor.shutdown();
  return result;
 }
}
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: ReactiveX/RxJava

@Override
public void run() {
  try {
    System.out.println("running TestMultiThreadedObservable thread");
    for (final String s : values) {
      threadPool.execute(new Runnable() {
    threadPool.shutdown();
  } catch (Throwable e) {
    throw new RuntimeException(e);
    threadPool.awaitTermination(2, TimeUnit.SECONDS);
  } catch (InterruptedException e) {
    throw new RuntimeException(e);
origin: stackoverflow.com

 final ExecutorService pool = Executors.newFixedThreadPool(2);
final CompletionService<String> service = new ExecutorCompletionService<String>(pool);
final List<? extends Callable<String>> callables = Arrays.asList(
  new SleepingCallable("slow", 5000),
  new SleepingCallable("quick", 500));
for (final Callable<String> callable : callables) {
 service.submit(callable);
}
pool.shutdown();
try {
 while (!pool.isTerminated()) {
  final Future<String> future = service.take();
  System.out.println(future.get());
 }
} catch (ExecutionException | InterruptedException ex) { }
origin: ReactiveX/RxJava

final int NUM_RETRIES = Flowable.bufferSize() * 2;
int ncpu = Runtime.getRuntime().availableProcessors();
ExecutorService exec = Executors.newFixedThreadPool(Math.max(ncpu / 2, 2));
try {
  for (int r = 0; r < NUM_LOOPS; r++) {
    if (r % 10 == 0) {
      System.out.println("testRetryWithBackpressureParallelLoop -> " + r);
  exec.shutdown();
java.util.concurrentExecutorServiceshutdown

Javadoc

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted. Invocation has no additional effect if already shut down.

This method does not wait for previously submitted tasks to complete execution. Use #awaitTerminationto do that.

Popular methods of ExecutorService

  • submit
    Submits a value-returning task for execution and returns a Future representing the pending results o
  • 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

  • Making http requests using okhttp
  • compareTo (BigDecimal)
  • onCreateOptionsMenu (Activity)
  • requestLocationUpdates (LocationManager)
  • Table (com.google.common.collect)
    A collection that associates an ordered pair of keys, called a row key and a column key, with a sing
  • Permission (java.security)
    Legacy security code; do not use.
  • Callable (java.util.concurrent)
    A task that returns a result and may throw an exception. Implementors define a single method with no
  • ZipFile (java.util.zip)
    This class provides random read access to a zip file. You pay more to read the zip file's central di
  • JLabel (javax.swing)
  • Join (org.hibernate.mapping)
  • Top Sublime Text plugins
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

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