Executor and ExecutorService API is a crucial tool for managing and controlling thread execution. They are part of java.util.concurrent package. They simplifies the process of concurrent programming by abstracting the complexities of thread creation, management, and synchronization.
Executors is a utility class from java.util.concurrent package provides factory methods for creating and managing different types of ExecutorService instances. It simplifies the process of creating thread pools and allows us to easily create and manage executor instances with different configurations.
Executor API It is an interface available since Java 1.5. It provides execute(Runnable command) method. This is base interface and ExecutorService extends this interface. The given command will be executed in future time by new thread or thread from thread pool or same thread and doesn't returns void.
ExecutorService API It is an interface available since Java 1.5. It provides multiple methods to control the execution of tasks in concurrent programming. It support both Runnable and Callable tasks. It returns Future for task status. Below are the most frequent used methods.
submit() accepts a Callable or a Runnable task and returns Future type result.
invokeAny() accepts a collection of tasks to run, and returns the result of a successful execution of any one task.
invokeAll() accepts a collection of tasks to run, and returns the result of all tasks in the form of a list of Future objects type.
shutdown() it doesn't stop executor service immediately but doesn't accept new tasks. Once all current running tasks finished, it shutdown the executor service.
shutdownNow() it tries to stop the executor service immediately, but it doesn't guarantee that all the running tasks will be stopped at the same time.
awaitTermination(long timeout, TimeUnit unit) blocks/waits until all tasks are completed or timeout occurs or current thread is interrupted, whichever happens first. The current thread will be blocked.
Types of ExecutorService
- FixedThreadPool It crates fixed size thread pool with specified number of threads. Tasks submitted are executed concurrently. If there are no tasks, threads will be idle until task is arrived. If threads are busy, tasks will be added queue.
ExecutorService fixedThreadPool = Executors.newScheduledThreadPool(5); Future<string> submit = fixedThreadPool.submit(() -> { System.out.println("Task executed by " + Thread.currentThread().getName()); return Thread.currentThread().getName(); }); fixedThreadPool.shutdown(); </string>
- CachedThreadPool Creates pool of threads and automatically adjust number of threads required in the pool based on workload. If thread is idle, for more than 60 seconds, will be terminated. This works well for dynamic loads. As threads will be killed after idle timeout, resources are better utilized here.
ExecutorService fixedThreadPool = Executors.newCachedThreadPool(); Future<string> submit = fixedThreadPool.submit(() -> { System.out.println("Task executed by " + Thread.currentThread().getName()); return Thread.currentThread().getName(); }); fixedThreadPool.shutdown(); </string>
- SingleThreadExecutor Creates single thread and tasks are executed sequentially. There is no parallel processing here.
ExecutorService fixedThreadPool = Executors.newSingleThreadExecutor(); Future<string> submit = fixedThreadPool.submit(() -> { System.out.println("Task executed by " + Thread.currentThread().getName()); return Thread.currentThread().getName(); }); fixedThreadPool.shutdown() </string>
- ScheduledThreadPool/ScheduledExecutor It creates a thread or trhead pool which will have a capability to run the tasks at regular interval or run after certain delay.
ScheduledExecutorService scheduler = Executors.newSingleThreadScheduledExecutor(); // Single-threaded scheduler ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(5); // Multi-threaded scheduler
scheduler.schedule(task, 10, TimeUnit.SECONDS); // Schedule task to run after 10 seconds. scheduler.scheduleAtFixedRate(task, 5, 10, TimeUnit.SECONDS); //It schedules a task to run every 10 seconds with an initial delay of 5 seconds. scheduler.scheduleWithFixedDelay(task, 5, 10, TimeUnit.SECONDS); //It schedules a task to run with a fixed delay of 10 seconds between the end of one execution and the start of the next, with an initial delay of 5 seconds. scheduler.schedule(() -> scheduler.shutdown(), 20, TimeUnit.SECONDS); //It schedules a shutdown of the scheduler after 20 seconds to stop the example.
Submitting Tasks to ExecutorService
Tasks can be submitted to the ExecutorService using the execute() and submit() methods. The execute() method is used for Runnable tasks, while submit() can handle both Runnable and Callable tasks."
executor.execute(new RunnableTask()); //fire-and-forgot executor.submit(new CallableTask()); //returns the status of task
Shutting Down ExecutorService
It's important to shut down the ExecutorService to release resources. You can do this using the shutdown() and shutdownNow() methods.
executor.shutdown(); // Initiates an orderly shutdown" executor.shutdownNow(); // Attempts to stop all actively executing tasks. executor.awaitTermination(long timeout, TimeUnit unit); //blocks the thread until all tasks are completed or timeout occurs or current thread is interrupted, whichever happens first. Returns `true `is tasks completed, otherwise `false`.
Recommended approach to shutdown
executor.shutdown(); try { // Wait for tasks to complete or timeout if (!executor.awaitTermination(120, TimeUnit.SECONDS)) { // If the timeout occurs, force shutdown executor.shutdownNow(); } } catch (InterruptedException ex) { executor.shutdownNow(); Thread.currentThread().interrupt(); }
About Runnable
- Runnable is an interface and represents a task that can run by threads.
- We can run Runnable tasks by using Threads or Executor service.
- Runnable is having run() method and doesn't return any data.
- We can't throw checked exception.
About Callable
- It is introduced in 1.5
- It is having call() method and returns type V.
- It contains throws Exception meaning, we can throw checked exception.
About Future
- It represents a future result of any task.
- The result will eventually appear in the Future after request processing is completed.
- boolean isDone() Returns the status of request processing. True if completed otherwise false.
- boolean cancel(boolean mayInterruptIfRunning) Cancels the submitted task. if we pass mayInterruptIfRunning as false, then it won't cancel already started task.
- boolean isCancelled() returns task is cancelled or not.
- V get() returns result of task. Blocks the thread if task is not completed.
- V get(long timeout, TimeUnit unit) Waits if necessary for at most the given time for the computation to complete, and then retrieves its result. TimeoutException will be thrown after specified time if computation is not completed.
Happy Coding and Learning !!!
Please drop a comment if you have any question.
The above is the detailed content of Overview of Executor Service in Java. For more information, please follow other related articles on the PHP Chinese website!

There are subtle differences in Java's performance on different operating systems. 1) The JVM implementations are different, such as HotSpot and OpenJDK, which affect performance and garbage collection. 2) The file system structure and path separator are different, so it needs to be processed using the Java standard library. 3) Differential implementation of network protocols affects network performance. 4) The appearance and behavior of GUI components vary on different systems. By using standard libraries and virtual machine testing, the impact of these differences can be reduced and Java programs can be ensured to run smoothly.

Javaoffersrobustobject-orientedprogramming(OOP)andtop-notchsecurityfeatures.1)OOPinJavaincludesclasses,objects,inheritance,polymorphism,andencapsulation,enablingflexibleandmaintainablesystems.2)SecurityfeaturesincludetheJavaVirtualMachine(JVM)forsand

JavaScriptandJavahavedistinctstrengths:JavaScriptexcelsindynamictypingandasynchronousprogramming,whileJavaisrobustwithstrongOOPandtyping.1)JavaScript'sdynamicnatureallowsforrapiddevelopmentandprototyping,withasync/awaitfornon-blockingI/O.2)Java'sOOPf

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM)andbytecode.1)TheJVMinterpretsbytecode,allowingthesamecodetorunonanyplatformwithaJVM.2)BytecodeiscompiledfromJavasourcecodeandisplatform-independent.However,limitationsincludepotentialp

Java'splatformindependencemeansapplicationscanrunonanyplatformwithaJVM,enabling"WriteOnce,RunAnywhere."However,challengesincludeJVMinconsistencies,libraryportability,andperformancevariations.Toaddressthese:1)Usecross-platformtestingtools,2)

JVM'sperformanceiscompetitivewithotherruntimes,offeringabalanceofspeed,safety,andproductivity.1)JVMusesJITcompilationfordynamicoptimizations.2)C offersnativeperformancebutlacksJVM'ssafetyfeatures.3)Pythonisslowerbuteasiertouse.4)JavaScript'sJITisles

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunonanyplatformwithaJVM.1)Codeiscompiledintobytecode,notmachine-specificcode.2)BytecodeisinterpretedbytheJVM,enablingcross-platformexecution.3)Developersshouldtestacross

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

SublimeText3 English version
Recommended: Win version, supports code prompts!

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

Dreamweaver CS6
Visual web development tools
