Basically on an ExecutorService
you call shutdown()
and then awaitTermination()
:
ExecutorService taskExecutor = Executors.newFixedThreadPool(4);
while(...) {
taskExecutor.execute(new MyTask());
}
taskExecutor.shutdown();
try {
taskExecutor.awaitTermination(Long.MAX_VALUE, TimeUnit.NANOSECONDS);
} catch (InterruptedException e) {
...
}
Related Contents:
- ExecutorService that interrupts tasks after a timeout
- Executors.newCachedThreadPool() versus Executors.newFixedThreadPool()
- What are the advantages of using an ExecutorService?
- Java’s Fork/Join vs ExecutorService – when to use which?
- Impossible to make a cached thread pool with a size limit?
- ThreadPoolExecutor Block When its Queue Is Full?
- What’s the difference between Thread start() and Runnable run()
- Why must wait() always be in synchronized block
- Why is creating a Thread said to be expensive?
- ExecutorService, how to wait for all tasks to finish
- What is the difference between Thread.start() and Thread.run()?
- Handling exceptions from Java ExecutorService tasks
- Synchronization vs Lock
- How to wait for a number of threads to complete?
- How to properly shutdown java ExecutorService
- Volatile vs Static in Java
- Are static variables shared between threads?
- Prime numbers by Eratosthenes quicker sequential than concurrently?
- How do synchronized static methods work in Java and can I use it for loading Hibernate entities?
- When would you call java’s thread.run() instead of thread.start()?
- Why use a ReentrantLock if one can use synchronized(this)?
- Choose between ExecutorService’s submit and ExecutorService’s execute
- Parallel streams, collectors and thread safety
- Java Executors: how can I set task priority?
- What are the main uses of yield(), and how does it differ from join() and interrupt()?
- How to properly use Java Executor?
- How to shutdown an ExecutorService?
- Volatile variable in Java
- Which concurrent Queue implementation should I use in Java?
- What is the JVM thread scheduling algorithm?
- What’s a monitor in Java?
- Turning an ExecutorService to daemon in Java
- Why should I use concurrent characteristic in parallel stream with collect?
- What is mutex and semaphore in Java ? What is the main difference?
- CountDownLatch vs. Semaphore
- FixedThreadPool vs CachedThreadPool: the lesser of two evils
- WAITING at sun.misc.Unsafe.park(Native Method)
- Constructor synchronization in Java
- Parallel execution of directed acyclic graph of tasks
- When should I use a CompletionService over an ExecutorService?
- Java : Does wait() release lock from synchronized block
- Java: ExecutorService that blocks on submission after a certain queue size [duplicate]
- Simplest and understandable example of volatile keyword in Java
- How to use invokeAll() to let all thread pool do their task?
- Programmatic deadlock detection in java
- When should we use Java’s Thread over Executor?
- Java: How to use Thread.join
- What is adaptive spinning w.r.t lock acquisition?
- How can I wrap a method so that I can kill its execution if it exceeds a specified timeout?
- Does Java have support for multicore processors/parallel processing?