Shutdown Java Executor After All Submitted Tasks Finished Without Blocking

zuckermanori

I'm wondering if there is a way to shutdown a Java ExecutorServiceand allow all submitted tasks to finish while not blocking.

to be more specific, I want to schedule a Runnable with a delay and continue with the code, without the need to keep a reference to the ExecutorService to be able to shut it down.

The following code will terminate the submitted task as it hasn't started yet:

ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.schedule(runnable, delay, TimeUnit.MILLISECONDS);
executor.shutdown();
...

While this code will block until the task is finished:

ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.schedule(runnable, delay, TimeUnit.MILLISECONDS);
executor.awaitTermination(timeout, TimeUnit.MILLISECONDS);
...

I want to have something like this:

ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
executor.schedule(runnable, delay, TimeUnit.MILLISECONDS);
executor.shutdownAfterTerminationWithoutBlocking();
...
code that runs without waiting

I know this possible using Timer but i'm wondering if this is possible using ExecutorService

Nikem

ExecutorService.shutdown javadoc says:

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.

So tasks that already submitted, but not yet started, will be executed. Exactly as you need.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Shutdown Java Executor After All Submitted Tasks Finished Without Blocking

From Dev

How to wait until all tasks finished without blocking UI thread?

From Dev

How to execute dependent tasks in Java 8 without any blocking

From Dev

Java executor with no ability to queue tasks

From Dev

How to wait for all tasks within a ThreadPoolExecutor to finish within a timeout and without shutting the Executor down?

From Dev

How to wait for all tasks within a ThreadPoolExecutor to finish within a timeout and without shutting the Executor down?

From Dev

C# async, Stop all tasks when one is finished without the access to the code

From Dev

Understanding java executor service shutdown and awaitTermination

From Dev

Understanding java executor service shutdown and awaitTermination

From Dev

GUI is freezed till all tasks are finished

From Dev

GUI is freezed till all tasks are finished

From Dev

Form showing only after async tasks finished

From Dev

ExecutorService's shutdown() doesn't wait until all threads will be finished

From Dev

Launch an application and wait until it has finished without blocking redraw

From Dev

Pause and Resume ExecutorService or shutdown and restart Java Executor service

From Dev

Remove and terminate all tasks from AsyncTask.THREAD_POOL_EXECUTOR

From Dev

How to wait until all tasks are finished before running code

From Dev

How to determine whether my Timer has finished all tasks?

From Dev

Node, async, and callback on all tasks finished within the iterator function

From Dev

How can I use a separate task executor for blocking sub tasks in netty?

From Dev

Blocking the output until input is finished using threads java

From Dev

RX Repeat after all subscribers finished

From Dev

How to shutdown CompletionService after completing currently executed tasks

From Dev

Comparison between shutting down an executor service (while awaiting termination) and wait cancellation of submitted tasks (using submit's futures)

From Dev

Java: How to get finished threads to pickup tasks from running threads

From Dev

How to add new tasks to threadpool only when all the tasks are finished using boost in c++

From Dev

Java hangs for a minute after a program is finished

From Dev

How to delete a file after thread is finished in java?

From Dev

Concurrent tasks on a Spark executor

Related Related

  1. 1

    Shutdown Java Executor After All Submitted Tasks Finished Without Blocking

  2. 2

    How to wait until all tasks finished without blocking UI thread?

  3. 3

    How to execute dependent tasks in Java 8 without any blocking

  4. 4

    Java executor with no ability to queue tasks

  5. 5

    How to wait for all tasks within a ThreadPoolExecutor to finish within a timeout and without shutting the Executor down?

  6. 6

    How to wait for all tasks within a ThreadPoolExecutor to finish within a timeout and without shutting the Executor down?

  7. 7

    C# async, Stop all tasks when one is finished without the access to the code

  8. 8

    Understanding java executor service shutdown and awaitTermination

  9. 9

    Understanding java executor service shutdown and awaitTermination

  10. 10

    GUI is freezed till all tasks are finished

  11. 11

    GUI is freezed till all tasks are finished

  12. 12

    Form showing only after async tasks finished

  13. 13

    ExecutorService's shutdown() doesn't wait until all threads will be finished

  14. 14

    Launch an application and wait until it has finished without blocking redraw

  15. 15

    Pause and Resume ExecutorService or shutdown and restart Java Executor service

  16. 16

    Remove and terminate all tasks from AsyncTask.THREAD_POOL_EXECUTOR

  17. 17

    How to wait until all tasks are finished before running code

  18. 18

    How to determine whether my Timer has finished all tasks?

  19. 19

    Node, async, and callback on all tasks finished within the iterator function

  20. 20

    How can I use a separate task executor for blocking sub tasks in netty?

  21. 21

    Blocking the output until input is finished using threads java

  22. 22

    RX Repeat after all subscribers finished

  23. 23

    How to shutdown CompletionService after completing currently executed tasks

  24. 24

    Comparison between shutting down an executor service (while awaiting termination) and wait cancellation of submitted tasks (using submit's futures)

  25. 25

    Java: How to get finished threads to pickup tasks from running threads

  26. 26

    How to add new tasks to threadpool only when all the tasks are finished using boost in c++

  27. 27

    Java hangs for a minute after a program is finished

  28. 28

    How to delete a file after thread is finished in java?

  29. 29

    Concurrent tasks on a Spark executor

HotTag

Archive