Java Threads & Concurrency API: Part 2


ExecutorService
Creating Threads with ExecutorService
In Part I of this article, we discussed what threads are in Java, how to create them, the concept of a task, and the differences between using the Callable
and Runnable
interfaces.
Let's delve a bit deeper into the Concurrency API, where Java introduced the ExecutorService
, which assists us in creating and managing threads.
ExecutorService
is an interface in Java. To create an instance and utilize this interface, the Concurrency API offers a factory class called "Executors."
Single-Thread Executor
Create an
ExecutorService
interface reference variable.Initialize it using
Executors
class. Let us use the simplest one →newSingleThreadExecutor()
.Create a task using
Runnable
and pass it to a method called on the instance created in the above step.
Output:
Task is smallest unit of work performed by a Thread
Second task
ExecutorService - Main
Third task
You can see that all the tasks using a single thread executor are in order, whereas the one by the Main
thread follows no sequence as it’s a separate thread. Hence, results are guaranteed to be executed in the order in which they are added to the executor service (for single thread executor).
But…this guarantee vanishes when the number of threads increases so it’s better not to code relying on this behavior.
Shutting Down a Thread Executor
Executor thread creates a non-daemon thread and hence it is important to call shutdown()
method once you are finished using the service.
It doesn't implement AutoCloseable interface so we cannot use it with try-with-resources either.
Note: In the next article we will see how to use submit()
instead of execute()
and the advantage of doing so.
Dear Readers,
I hope you are enjoying the content I provide on my blog. As a passionate writer and dedicated software developer, I strive to create valuable and informative articles that resonate with you. Today, I would like to extend an invitation to support my work and help me continue producing high-quality content.
I have set up a Buy Me a Coffee page, a platform that allows readers like you to show their appreciation by making a small donation. Your contribution, no matter how big or small, goes a long way in supporting my efforts and keeping the blog running. You can also sponsor using the links at the bottom of this page.
Subscribe to my newsletter
Read articles from Ish Mishra directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Ish Mishra
Ish Mishra
Welcome to Bits8Byte! I’m Ish, a seasoned Software Engineer with 11+ years of experience in software development, automation, and AI/ML. I have a deep passion for technology, problem-solving, and continuous learning, and I created this blog to share my insights, experiences, and discoveries in the ever-evolving world of software engineering. Throughout my career, I’ve worked extensively with Java (Spring Boot), Python (FastAPI), AI/ML, Cloud Computing (AWS), DevOps, Docker, Kubernetes, and Test Automation frameworks. My journey has led me to explore microservices architecture, API development, observability (OpenTelemetry, Prometheus), and AI-powered solutions. On this blog, you’ll find practical tutorials, in-depth technical discussions, and real-world problem-solving strategies. I’ll also share my experiences working on high-performance microservices, AI applications, cloud deployments, and automation frameworks, along with best practices to help fellow developers and engineers. I encourage you to join the conversation—leave comments, ask questions, and share your thoughts! Let’s learn, innovate, and grow together in this exciting journey of software development.