SlideShare a Scribd company logo
Efficient Java Multithreading – Using
Executors to do everything that you can
do with Threads!
- By Arun K. Mehra
Java Multithreading Using Executors Framework
Disclaimer
 This presentation does not cover the basics of Java Multithreading
 It’s focus is primarily on the Executors Framework
 It is used in the online video course on Udemy – “Efficient Java
Multithreading with Executors”
 It’s probably a bit boring as it contains a lot of text but no funny
images and no code listings 
 The matter in the slides is actually explained in the video course
(sample videos can be viewed on You-Tube or Udemy)
 Still strong? Let’s roll! 
About me!
 My name is Arun Kumar Mehra.
 Nearly 13 years in Software Industry architecting and
developing Enterprise applications
 Worked in diverse domains – Life Insurance, Investment
Banking and Telecommunications
 Mostly used Java/JEE technologies
 Diehard fan of Open Source especially, Java, Groovy,
OSGi and Eclipse RCP/RAP
Pre-requisites
Good knowledge of Java and basic knowledge of
multithreading required …
o Why multithreading
o Scenarios where multithreading is useful (and where it is not!)
o Difference between thread-of-execution and Thread class
o Various execution-thread states
o Thread class operations
o Etc.
Course Structure
Introduction
Creating and Running threads
Naming threads
Returning values from threads
Creating daemon threads
Checking threads for ‘life’
01
02
03
04
05
06
Terminating threads
Handling Uncaught Exceptions
Waiting for other threads to
terminate
Scheduling tasks
07
08
09
10
02. CREATING & RUNNING THREADS
Starting Threads – Basic Mechanism
 Thread class object
 Runnable interface implementation or ‘The Task’
 Invocation of start() method on the Thread object
To run a task in a separate thread-of-execution, three
things are required:
Creating Threads Using Threads API
Quite a few ways. Difference lies in:
 The mechanism using which:
o The Thread object is created
o The Task is created
 The way these two objects are brought together and made to
collaborate
Running Threads Using Executors API
Only one way!
 Create task definition class
 Provide task object to an Executor Service
Executors API Overview
Preferred way of running tasks...
 Uses thread-pools
 Allocates heavy-weight threads upfront
 Decouples task-submission from thread-creation-and-management
 Each thread in the pool executes multiple tasks one-by-one
 A tasks-queue holds the tasks
 Threads are stopped when the Executor Service is stopped
Important Classes/Interfaces
Some frequently referred classes/interfaces while using
Executors framework:
 Executor (I)
void execute (Runnable task);
Important Classes/Interfaces
Some frequently referred classes/interfaces while using
Executors framework:
 Executor (I)
 ExecutorService (I)
<T> Future<T> submit (Callable<T> task);
Future<?> submit (Runnable task);
void shutdown ( );
List<Runnable> shutdownNow ( );
boolean isShutdown ( );
…
Important Classes/Interfaces
Some frequently referred classes/interfaces while using
Executors framework:
 Executor (I)
 ExecutorService (I)
 Executors (C)
public static ExecutorService newFixedThreadPool (int nThreads);
public static ExecutorService newCachedThreadPool ( );
public static ExecutorService newSingleThreadExecutor ( );
public static ExecutorService newSingleThreadScheduledExecutor ( );
…
Important Classes/Interfaces
Some frequently referred classes/interfaces while using
Executors framework:
 Executor (I)
 ExecutorService (I)
 Executors (C)
 Future (I)
V get ( ) throws InterruptedException , ExecutionException;
boolean isDone ( );
…
Available Thread-Pools
Many types of thread-pools available out-of-the-box:
 Fixed Thread Pool
 Cached Thread Pool
 Single Thread Executor (technically not a ‘pool’)
 Scheduled Thread Pool
 Single Thread Scheduled Executor (technically not a ‘pool’)
03. NAMING THREADS
What’s In A Name?
 ‘Thread-0’, ‘Thread-1’, ‘Thread-2’ – not very helpful in identifying
the threads
 More useful names would be:
o Directory-Scanner-Thread
o Timer-Thread
o Cache-Invalidating-Thread
 Helps in identifying special threads while debugging multi-threaded
applications
Why would you want to name threads …
Naming Normal Threads
 From within the task – at ‘thread-running’ time
 At ‘thread-creation’ time
Java provides a couple of opportunities …
Naming Executor Threads
 From within the task - at ‘thread-running’ time. Similar to the
technique that was used for renaming normal threads.
 Specify the thread-naming strategy at ‘executor-creation’ time
Two ways …
Default Names of Executor Threads
“pool-N-thread-M”
Internal pool-sequence-no. in the
JVM. Incremented every time a new
executor-pool is created!
Internal thread-sequence-no. inside
an executor-pool. Incremented every
time a new thread is created!
04. RETURNING VALUES FROM THREADS
Returning Values Using Normal Threads
 No language level support in normal Threads but there is support
in Executors
 A few ways for normal threads – see code
CTM: Values are returned from Tasks and not Threads!
Returning Values Using Executors
 Use Callable<T> instead of Runnable
o Override method: public T call() throws Exception
 ExecutorService.submit(Callable c)
 Call to submit() returns a Future<T>
 Task result can be retrieved using “T Future.get()” method
Out-of-the-box support provided by Java …
Processing Tasks’ Results in Order of Completion
 CompletionService<V> (I)
 ExecutorCompletionService<V> (C)
Use the following …
Future<V> submit (Callable<V> task)
Future<V> submit (Runnable task, V result)
Future<V> take ( )
Future<V> poll ( )
05. DAEMON THREADS
Daemon Threads – What and Why?
 Killed by the JVM as soon as no user thread is running any longer
o Can be stopped normally too – just like normal threads
 ‘main’ thread is a ‘user’ thread – not ‘daemon’
 Any kind of logic as desired can be put in daemon threads. E.g.:
o Directory-watcher-thread
o Socket-reader-thread
o Long-calculation-thread
Background and/or service providing threads …
Creating ‘Daemon’ Threads
 void Thread.setDaemon(boolean on)
o Also used in Executors API
 For Executors, implement a ThreadFactory and manipulate the
thread there to make it ‘daemon’
Thread class provides a method …
06. CHECKING THREADS FOR ‘LIFE’
Checking Threads for ‘Life’ - Threads API
 Use the method: boolean Thread.isAlive()
 Will return true if:
o … the thread is still running
 Will return false in the following scenarios:
o If the thread has not been started yet i.e Thread.start() has not been called yet or;
o If the thread has terminated – irrespective of whether it has successfully finished
executing its task or not
A thread is live if it has started but not terminated yet!!!
Checking Threads for ‘Life’ – Executors API
 Actually we are interested in task completion!!!
 Use the method: boolean Future.isDone()
 Will return true if:
o … the task execution is completed - whether normally, or with exception or with
cancellation
 Will return false if:
o … the task execution has not been completed yet – in fact, it may not even have
started yet!
Are we really interested in a thread being live …???
This deck is part of the full-fledged video course on :
EFFICIENT JAVA MULTITHREADING
WITH
EXECUTORS
Visit: Course
07. TERMINATING THREADS
Terminating Normal Threads
 Terminating at non-blocked portions in the task:
o Using a method coded for this purpose in the task
o Using interrupts:
 void Thread.interrupt() – usually called by another thread on the thread-to-be-
interrupted
 static boolean Thread.interrupted() – to be called from inside the interrupted thread
 boolean Thread.isInterrupted() – to be called by another thread on the interrupted
thread
 Terminating at points in the task where the thread is
blocked:
o Using interrupts – same methods as above
There are a few ways …
Terminating Individual Executor Tasks
 Terminating at non-blocked portions in the task:
o Using a method coded for this purpose in the task
o Using interrupts:
 boolean Future.cancel(boolean mayInterruptIfRunning) – to be called by the class holding
the Future ref.
 static boolean Thread.interrupted() – to be called from inside the interrupted task
 boolean Future.isCancelled() – to be called on the Future outside the interrupted task
 Terminating at points in the task where the thread is
blocked:
o Using interrupts – same methods as above
There are a few ways …
Terminating All Executor Tasks in One-Shot
 Use: List<Runnable> ExecutorService.shutdownNow()
o Uses interrupts under the hood
o Returns a list of tasks that were awaiting execution yet
 Interrupting may terminate blocked as well as non-blocked tasks
 Tasks must be coded to properly handle the interrupts
 To await termination after shutting down, use:
o boolean ExecService.awaitTermination(long timeout, TimeUnit unit)
Uses the same concepts as discussed previously …
08. HANDLING UNCAUGHT EXCEPTIONS
Uncaught Exceptions in Threads
 Implement Thread.UncaughtExceptionHandler interface
o Only one method: void uncaughtException(Thread t, Throwable e)
 Three ways to use UncaughtExceptionHandler:
o Set as default handler for all the threads in the system
 static void Thread.setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler eh)
o Set different handlers for different threads
 void Thread.setUncaughtExceptionHandler(UncaughtExceptionHandler eh)
o Use a combination of default-handler and thread-specific-handlers
Leaking Exceptions from threads cannot be caught
directly …
Uncaught Exceptions in Executors
 Implement Thread.UncaughtExceptionHandler interface
o Only one method: void uncaughtException(Thread t, Throwable e)
 Three ways to use UncaughtExceptionHandler:
o Set as default handler for all the threads in the system
 static void Thread.setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler eh)
o Set different handlers for different threads
 Implement a ThreadFactory and;
 Use void Thread.setUncaughtExceptionHandler(UncaughtExceptionHandler eh)
o Use a combination of default-handler and thread-specific-handlers
Similar ways of handling like the normal threads …
09. WAITING FOR OTHER THREADS TO
COMPLETE
Waiting for Normal-Threads to Finish
 Previously discussed techniques:
o boolean Thread.isAlive()
 To be called multiple times till you get a ‘false’
 Wastage of CPU cycles
o wait() and notify() mechanism
 To be coded manually in the tasks by the programmer
 A little difficult to get right
 Third technique:
o void Thread.join()
 In-built mechanism – works just like wait() and notify()
 Easy to use
Suspend the current thread so that it continues only after some
other thread(s) have finished …
Waiting for Executor Tasks to Finish
 Use java.util.concurrent.CountDownLatch
 An object of CountDownLatch is shared by the waiting tasks and
the awaited tasks.
o Waiting-tasks call void await() on the latch
o Awaited-tasks call void countdown() on the latch after finishing their
executions
o When the count reaches zero, waiting tasks are released, thereby, bringing
them out of suspended state and enabling their execution again.
Suspend the current task so that it continues only after some
other task(s) have finished …
10. SCHEDULING TASKS
Scheduling Tasks using Threads-API
 java.util.Timer
o Spawns a thread for executing the tasks
o Also contains scheduling logic
 java.util.TimerTask
o Implements Runnable interface
o Represents the task
o Should be short-lived for repeated
executions
For all scheduling needs, Threads-API provides two classes …
void schedule(TimerTask task, Date time)
void schedule(TimerTask task, long delay)
void schedule(TimerTask task, Date firstTime, long period)
void schedule(TimerTask task, long delay, long period)
void scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
void scheduleAtFixedRate(TimerTask task, long delay, long period)
void cancel()
long scheduledExecutionTime()
boolean cancel()
The “java.util.Timer” Class
 Single task-execution-thread per Timer object
 Timer tasks should complete quickly
 Always call Timer.cancel() when application is shutting down;
otherwise it may cause memory leak
 Don’t schedule any more tasks on the Timer after cancelling it
 Timer class is thread-safe
 Timer class does not offer any real-time guarantees
Few things worth stressing upon about the Timer class …
One-Time Execution in Future - Threads-API
 Two Timer methods for scheduling one-time execution:
o void schedule(TimerTask task, Date time)
o void schedule(TimerTask task, long delay)
Use Timer and TimerTask classes …
Repeated Fixed-Delay Executions – Threads API
Timer and TimerTask only, are used …
 Two methods in Timer class:
o void schedule(TimerTask task, long delay, long period)
o void schedule(TimerTask task, Date firstTime, long period)
 Next execution is scheduled when the current one begins
 Each execution is schedule relative to the actual start time of the
previous one
 Start-times drift forward over long runs
o Hence, frequency of executions becomes lower than that specified
 Appropriate for activities that require ‘smoothness’ over short
periods of time e.g. blinking cursor, hourly chimes, etc.
Repeated Fixed-Rate Executions – Threads API
Timer and TimerTask only, are used …
 Two methods in Timer class:
o void scheduleAtFixedRate(TimerTask task, long delay, long period)
o void scheduleAtFixedRate(TimerTask task, Date firstTime, long period)
 Next execution is scheduled when the current one begins
 Each execution is schedule relative to the scheduled start time of
the first execution
 Start-times DO NOT drift forward over long runs
o Hence, frequency of executions remains constant
 Appropriate for activities that are sensitive to ‘absolute time’ and
accurate frequency of executions
Scheduling Tasks - Executors-API
 Available pools:
o Single-thread-scheduled-executor
o Scheduled-thread-pool
 Created using Executors (C)
 ScheduledExecutorService (I)
o Extends ExecutorService (I)
 ScheduledFuture (I). Extends:
o Future (I)
o Delayed (I)
Special thread-pool(s) provided by Executors for scheduling tasks …
ScheduledFuture<?> schedule(Runnable command, long delay,
TimeUnit unit)
<V> ScheduledFuture<V> schedule(Callable<V> callable, long delay,
TimeUnit unit)
ScheduledFuture<?> scheduleWithFixedDelay(Runnable command,
long initialDelay, long delay, TimeUnit unit)
ScheduledFuture<?> scheduleAtFixedRate(Runnable command,
long initialDelay, long period, TimeUnit unit)
static ScheduledExecutorService newSingleThreadScheduledExecutor()
static ScheduledExecutorService newScheduledThreadPool(int corePoolSize)
long getDelay(TimeUnit unit)
One Time Execution in Future – Executors API
ScheduledExecutorService is used …
 Two methods:
o ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit)
o ScheduledFuture<?> schedule(Callable<V> callable, long delay, TimeUnit unit)
 Executions can be scheduled using a single-thread-executor or
scheduled-thread-pool
 No need to extend TimerTask. Normal Runnables and Callables only
are needed!
Repeated Fixed-Delay Executions – Executors API
ScheduledExecutorService is used …
 Only one method:
o ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long
initialDelay, long delay, TimeUnit unit)
 Executions can be scheduled using a single-thread-executor or
scheduled-thread-pool
 Each execution is schedule relative to the termination-time of the
previous execution
o Start-times drift forward over long runs
o Hence, frequency of executions becomes lower than that specified
 Only Runnables can be scheduled – no Callables!
Repeated Fixed-Rate Executions – Executors API
ScheduledExecutorService is used …
 Only one method:
o ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long
initialDelay, long period, TimeUnit unit)
 Executions can be scheduled using a single-thread-executor or
scheduled-thread-pool
 Each execution is schedule relative to the scheduled start time of
the first execution
o Start-times DO NOT drift forward over long runs
o Hence, frequency of executions remains constant
 Only Runnables can be scheduled – no Callables!
THANK YOU !!!
This deck is part of the full-fledged video course on :
EFFICIENT JAVA MULTITHREADING
WITH
EXECUTORS
Visit: Course

More Related Content

What's hot (20)

Spring boot
Spring boot
Gyanendra Yadav
 
Spring boot
Spring boot
Pradeep Shanmugam
 
9. Input Output in java
9. Input Output in java
Nilesh Dalvi
 
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
An Introduction to JUnit 5 and how to use it with Spring boot tests and Mockito
shaunthomas999
 
Java Exception handling
Java Exception handling
kamal kotecha
 
Spring AOP
Spring AOP
Tata Consultancy Services
 
ArrayList in JAVA
ArrayList in JAVA
SAGARDAVE29
 
Exception handling in java
Exception handling in java
Pratik Soares
 
Spring Boot
Spring Boot
HongSeong Jeon
 
Vectors in Java
Vectors in Java
Abhilash Nair
 
Super keyword in java
Super keyword in java
Hitesh Kumar
 
Inheritance in java
Inheritance in java
Lovely Professional University
 
Multi threading
Multi threading
gndu
 
Generics
Generics
Ravi_Kant_Sahu
 
Abstract class in java
Abstract class in java
Lovely Professional University
 
Arrays in Java
Arrays in Java
Abhilash Nair
 
Interfaces in java
Interfaces in java
Shiv Mehmi
 
GUI Programming In Java
GUI Programming In Java
yht4ever
 
Collections framework in java
Collections framework in java
yugandhar vadlamudi
 
Threads in JAVA
Threads in JAVA
Haldia Institute of Technology
 

Viewers also liked (20)

Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVA
Vikram Kalyani
 
Java And Multithreading
Java And Multithreading
Shraddha
 
Multithreading In Java
Multithreading In Java
parag
 
Multithreading in java
Multithreading in java
Raghu nath
 
Java 8 Features
Java 8 Features
Trung Nguyen
 
Networking in java
Networking in java
shravan kumar upadhayay
 
Major Java 8 features
Major Java 8 features
Sanjoy Kumar Roy
 
Java Networking
Java Networking
Sunil OS
 
Java Generics Introduction - Syntax Advantages and Pitfalls
Java Generics Introduction - Syntax Advantages and Pitfalls
Rakesh Waghela
 
Junit 4.0
Junit 4.0
pallavikhandekar212
 
Java Input Output (java.io.*)
Java Input Output (java.io.*)
Om Ganesh
 
Networking in Java
Networking in Java
Tushar B Kute
 
Java Persistence API (JPA) - A Brief Overview
Java Persistence API (JPA) - A Brief Overview
Craig Dickson
 
JUnit- A Unit Testing Framework
JUnit- A Unit Testing Framework
Onkar Deshpande
 
Java 8 new features
Java 8 new features
Aniket Thakur
 
Operating System Chapter 4 Multithreaded programming
Operating System Chapter 4 Multithreaded programming
guesta40f80
 
java networking
java networking
Waheed Warraich
 
Networking Java Socket Programming
Networking Java Socket Programming
Mousmi Pawar
 
Unit testing with JUnit
Unit testing with JUnit
Thomas Zimmermann
 
Java Input Output and File Handling
Java Input Output and File Handling
Sunil OS
 
Multi-threaded Programming in JAVA
Multi-threaded Programming in JAVA
Vikram Kalyani
 
Java And Multithreading
Java And Multithreading
Shraddha
 
Multithreading In Java
Multithreading In Java
parag
 
Multithreading in java
Multithreading in java
Raghu nath
 
Java Networking
Java Networking
Sunil OS
 
Java Generics Introduction - Syntax Advantages and Pitfalls
Java Generics Introduction - Syntax Advantages and Pitfalls
Rakesh Waghela
 
Java Input Output (java.io.*)
Java Input Output (java.io.*)
Om Ganesh
 
Java Persistence API (JPA) - A Brief Overview
Java Persistence API (JPA) - A Brief Overview
Craig Dickson
 
JUnit- A Unit Testing Framework
JUnit- A Unit Testing Framework
Onkar Deshpande
 
Operating System Chapter 4 Multithreaded programming
Operating System Chapter 4 Multithreaded programming
guesta40f80
 
Networking Java Socket Programming
Networking Java Socket Programming
Mousmi Pawar
 
Java Input Output and File Handling
Java Input Output and File Handling
Sunil OS
 
Ad

Similar to Java Multithreading Using Executors Framework (20)

Lecture 23-24.pptx
Lecture 23-24.pptx
talha ijaz
 
Class notes(week 9) on multithreading
Class notes(week 9) on multithreading
Kuntal Bhowmick
 
Multithreading.pptx
Multithreading.pptx
PragatiSutar4
 
Slide 7 Thread-1.pptx
Slide 7 Thread-1.pptx
ajmalhamidi1380
 
Thread
Thread
Mohamed Essam
 
Multi-Threading in Java power point presenetation
Multi-Threading in Java power point presenetation
AshokRachapalli1
 
Multi Threading
Multi Threading
Ferdin Joe John Joseph PhD
 
Java concurrency
Java concurrency
Abhijit Gaikwad
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
Kartik Dube
 
Threadnotes
Threadnotes
Himanshu Rajput
 
Class notes(week 9) on multithreading
Class notes(week 9) on multithreading
Kuntal Bhowmick
 
JAVA MULTITHREDED PROGRAMMING - LECTURES
JAVA MULTITHREDED PROGRAMMING - LECTURES
rm170484
 
web programming-Multithreading concept in Java.ppt
web programming-Multithreading concept in Java.ppt
mcjaya2024
 
Concurrency Programming in Java - 07 - High-level Concurrency objects, Lock O...
Concurrency Programming in Java - 07 - High-level Concurrency objects, Lock O...
Sachintha Gunasena
 
Java Multithreading - how to create threads
Java Multithreading - how to create threads
ManishKumar475693
 
Java threads
Java threads
javaicon
 
Multi threading
Multi threading
Ravi Kant Sahu
 
L22 multi-threading-introduction
L22 multi-threading-introduction
teach4uin
 
Concurrent Programming in Java
Concurrent Programming in Java
Ruben Inoto Soto
 
[Java concurrency]01.thread management
[Java concurrency]01.thread management
xuehan zhu
 
Lecture 23-24.pptx
Lecture 23-24.pptx
talha ijaz
 
Class notes(week 9) on multithreading
Class notes(week 9) on multithreading
Kuntal Bhowmick
 
Multi-Threading in Java power point presenetation
Multi-Threading in Java power point presenetation
AshokRachapalli1
 
Multithreading Introduction and Lifecyle of thread
Multithreading Introduction and Lifecyle of thread
Kartik Dube
 
Class notes(week 9) on multithreading
Class notes(week 9) on multithreading
Kuntal Bhowmick
 
JAVA MULTITHREDED PROGRAMMING - LECTURES
JAVA MULTITHREDED PROGRAMMING - LECTURES
rm170484
 
web programming-Multithreading concept in Java.ppt
web programming-Multithreading concept in Java.ppt
mcjaya2024
 
Concurrency Programming in Java - 07 - High-level Concurrency objects, Lock O...
Concurrency Programming in Java - 07 - High-level Concurrency objects, Lock O...
Sachintha Gunasena
 
Java Multithreading - how to create threads
Java Multithreading - how to create threads
ManishKumar475693
 
Java threads
Java threads
javaicon
 
L22 multi-threading-introduction
L22 multi-threading-introduction
teach4uin
 
Concurrent Programming in Java
Concurrent Programming in Java
Ruben Inoto Soto
 
[Java concurrency]01.thread management
[Java concurrency]01.thread management
xuehan zhu
 
Ad

Recently uploaded (20)

Artificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdf
OnBoard
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
The State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry Report
Liveplex
 
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
Safe Software
 
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
Muhammad Rizwan Akram
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
Safe Software
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
Edge AI and Vision Alliance
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
High Availability On-Premises FME Flow.pdf
High Availability On-Premises FME Flow.pdf
Safe Software
 
PyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent Integration
barqawicloud
 
Kubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too Late
Michael Furman
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 
Artificial Intelligence in the Nonprofit Boardroom.pdf
Artificial Intelligence in the Nonprofit Boardroom.pdf
OnBoard
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
The State of Web3 Industry- Industry Report
The State of Web3 Industry- Industry Report
Liveplex
 
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
FME for Distribution & Transmission Integrity Management Program (DIMP & TIMP)
Safe Software
 
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
ENERGY CONSUMPTION CALCULATION IN ENERGY-EFFICIENT AIR CONDITIONER.pdf
Muhammad Rizwan Akram
 
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Seminar: Perspectives on Passkeys & Consumer Adoption.pptx
FIDO Alliance
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
MuleSoft for AgentForce : Topic Center and API Catalog
MuleSoft for AgentForce : Topic Center and API Catalog
shyamraj55
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
cnc-drilling-dowel-inserting-machine-drillteq-d-510-english.pdf
AmirStern2
 
Crypto Super 500 - 14th Report - June2025.pdf
Crypto Super 500 - 14th Report - June2025.pdf
Stephen Perrenod
 
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
FME for Good: Integrating Multiple Data Sources with APIs to Support Local Ch...
Safe Software
 
Down the Rabbit Hole – Solving 5 Training Roadblocks
Down the Rabbit Hole – Solving 5 Training Roadblocks
Rustici Software
 
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
“Addressing Evolving AI Model Challenges Through Memory and Storage,” a Prese...
Edge AI and Vision Alliance
 
Murdledescargadarkweb.pdfvolumen1 100 elementary
Murdledescargadarkweb.pdfvolumen1 100 elementary
JorgeSemperteguiMont
 
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Viral>Wondershare Filmora 14.5.18.12900 Crack Free Download
Puppy jhon
 
High Availability On-Premises FME Flow.pdf
High Availability On-Premises FME Flow.pdf
Safe Software
 
PyData - Graph Theory for Multi-Agent Integration
PyData - Graph Theory for Multi-Agent Integration
barqawicloud
 
Kubernetes Security Act Now Before It’s Too Late
Kubernetes Security Act Now Before It’s Too Late
Michael Furman
 
Bridging the divide: A conversation on tariffs today in the book industry - T...
Bridging the divide: A conversation on tariffs today in the book industry - T...
BookNet Canada
 

Java Multithreading Using Executors Framework

  • 1. Efficient Java Multithreading – Using Executors to do everything that you can do with Threads! - By Arun K. Mehra
  • 3. Disclaimer  This presentation does not cover the basics of Java Multithreading  It’s focus is primarily on the Executors Framework  It is used in the online video course on Udemy – “Efficient Java Multithreading with Executors”  It’s probably a bit boring as it contains a lot of text but no funny images and no code listings   The matter in the slides is actually explained in the video course (sample videos can be viewed on You-Tube or Udemy)  Still strong? Let’s roll! 
  • 4. About me!  My name is Arun Kumar Mehra.  Nearly 13 years in Software Industry architecting and developing Enterprise applications  Worked in diverse domains – Life Insurance, Investment Banking and Telecommunications  Mostly used Java/JEE technologies  Diehard fan of Open Source especially, Java, Groovy, OSGi and Eclipse RCP/RAP
  • 5. Pre-requisites Good knowledge of Java and basic knowledge of multithreading required … o Why multithreading o Scenarios where multithreading is useful (and where it is not!) o Difference between thread-of-execution and Thread class o Various execution-thread states o Thread class operations o Etc.
  • 6. Course Structure Introduction Creating and Running threads Naming threads Returning values from threads Creating daemon threads Checking threads for ‘life’ 01 02 03 04 05 06 Terminating threads Handling Uncaught Exceptions Waiting for other threads to terminate Scheduling tasks 07 08 09 10
  • 7. 02. CREATING & RUNNING THREADS
  • 8. Starting Threads – Basic Mechanism  Thread class object  Runnable interface implementation or ‘The Task’  Invocation of start() method on the Thread object To run a task in a separate thread-of-execution, three things are required:
  • 9. Creating Threads Using Threads API Quite a few ways. Difference lies in:  The mechanism using which: o The Thread object is created o The Task is created  The way these two objects are brought together and made to collaborate
  • 10. Running Threads Using Executors API Only one way!  Create task definition class  Provide task object to an Executor Service
  • 11. Executors API Overview Preferred way of running tasks...  Uses thread-pools  Allocates heavy-weight threads upfront  Decouples task-submission from thread-creation-and-management  Each thread in the pool executes multiple tasks one-by-one  A tasks-queue holds the tasks  Threads are stopped when the Executor Service is stopped
  • 12. Important Classes/Interfaces Some frequently referred classes/interfaces while using Executors framework:  Executor (I) void execute (Runnable task);
  • 13. Important Classes/Interfaces Some frequently referred classes/interfaces while using Executors framework:  Executor (I)  ExecutorService (I) <T> Future<T> submit (Callable<T> task); Future<?> submit (Runnable task); void shutdown ( ); List<Runnable> shutdownNow ( ); boolean isShutdown ( ); …
  • 14. Important Classes/Interfaces Some frequently referred classes/interfaces while using Executors framework:  Executor (I)  ExecutorService (I)  Executors (C) public static ExecutorService newFixedThreadPool (int nThreads); public static ExecutorService newCachedThreadPool ( ); public static ExecutorService newSingleThreadExecutor ( ); public static ExecutorService newSingleThreadScheduledExecutor ( ); …
  • 15. Important Classes/Interfaces Some frequently referred classes/interfaces while using Executors framework:  Executor (I)  ExecutorService (I)  Executors (C)  Future (I) V get ( ) throws InterruptedException , ExecutionException; boolean isDone ( ); …
  • 16. Available Thread-Pools Many types of thread-pools available out-of-the-box:  Fixed Thread Pool  Cached Thread Pool  Single Thread Executor (technically not a ‘pool’)  Scheduled Thread Pool  Single Thread Scheduled Executor (technically not a ‘pool’)
  • 18. What’s In A Name?  ‘Thread-0’, ‘Thread-1’, ‘Thread-2’ – not very helpful in identifying the threads  More useful names would be: o Directory-Scanner-Thread o Timer-Thread o Cache-Invalidating-Thread  Helps in identifying special threads while debugging multi-threaded applications Why would you want to name threads …
  • 19. Naming Normal Threads  From within the task – at ‘thread-running’ time  At ‘thread-creation’ time Java provides a couple of opportunities …
  • 20. Naming Executor Threads  From within the task - at ‘thread-running’ time. Similar to the technique that was used for renaming normal threads.  Specify the thread-naming strategy at ‘executor-creation’ time Two ways …
  • 21. Default Names of Executor Threads “pool-N-thread-M” Internal pool-sequence-no. in the JVM. Incremented every time a new executor-pool is created! Internal thread-sequence-no. inside an executor-pool. Incremented every time a new thread is created!
  • 22. 04. RETURNING VALUES FROM THREADS
  • 23. Returning Values Using Normal Threads  No language level support in normal Threads but there is support in Executors  A few ways for normal threads – see code CTM: Values are returned from Tasks and not Threads!
  • 24. Returning Values Using Executors  Use Callable<T> instead of Runnable o Override method: public T call() throws Exception  ExecutorService.submit(Callable c)  Call to submit() returns a Future<T>  Task result can be retrieved using “T Future.get()” method Out-of-the-box support provided by Java …
  • 25. Processing Tasks’ Results in Order of Completion  CompletionService<V> (I)  ExecutorCompletionService<V> (C) Use the following … Future<V> submit (Callable<V> task) Future<V> submit (Runnable task, V result) Future<V> take ( ) Future<V> poll ( )
  • 27. Daemon Threads – What and Why?  Killed by the JVM as soon as no user thread is running any longer o Can be stopped normally too – just like normal threads  ‘main’ thread is a ‘user’ thread – not ‘daemon’  Any kind of logic as desired can be put in daemon threads. E.g.: o Directory-watcher-thread o Socket-reader-thread o Long-calculation-thread Background and/or service providing threads …
  • 28. Creating ‘Daemon’ Threads  void Thread.setDaemon(boolean on) o Also used in Executors API  For Executors, implement a ThreadFactory and manipulate the thread there to make it ‘daemon’ Thread class provides a method …
  • 29. 06. CHECKING THREADS FOR ‘LIFE’
  • 30. Checking Threads for ‘Life’ - Threads API  Use the method: boolean Thread.isAlive()  Will return true if: o … the thread is still running  Will return false in the following scenarios: o If the thread has not been started yet i.e Thread.start() has not been called yet or; o If the thread has terminated – irrespective of whether it has successfully finished executing its task or not A thread is live if it has started but not terminated yet!!!
  • 31. Checking Threads for ‘Life’ – Executors API  Actually we are interested in task completion!!!  Use the method: boolean Future.isDone()  Will return true if: o … the task execution is completed - whether normally, or with exception or with cancellation  Will return false if: o … the task execution has not been completed yet – in fact, it may not even have started yet! Are we really interested in a thread being live …???
  • 32. This deck is part of the full-fledged video course on : EFFICIENT JAVA MULTITHREADING WITH EXECUTORS Visit: Course
  • 34. Terminating Normal Threads  Terminating at non-blocked portions in the task: o Using a method coded for this purpose in the task o Using interrupts:  void Thread.interrupt() – usually called by another thread on the thread-to-be- interrupted  static boolean Thread.interrupted() – to be called from inside the interrupted thread  boolean Thread.isInterrupted() – to be called by another thread on the interrupted thread  Terminating at points in the task where the thread is blocked: o Using interrupts – same methods as above There are a few ways …
  • 35. Terminating Individual Executor Tasks  Terminating at non-blocked portions in the task: o Using a method coded for this purpose in the task o Using interrupts:  boolean Future.cancel(boolean mayInterruptIfRunning) – to be called by the class holding the Future ref.  static boolean Thread.interrupted() – to be called from inside the interrupted task  boolean Future.isCancelled() – to be called on the Future outside the interrupted task  Terminating at points in the task where the thread is blocked: o Using interrupts – same methods as above There are a few ways …
  • 36. Terminating All Executor Tasks in One-Shot  Use: List<Runnable> ExecutorService.shutdownNow() o Uses interrupts under the hood o Returns a list of tasks that were awaiting execution yet  Interrupting may terminate blocked as well as non-blocked tasks  Tasks must be coded to properly handle the interrupts  To await termination after shutting down, use: o boolean ExecService.awaitTermination(long timeout, TimeUnit unit) Uses the same concepts as discussed previously …
  • 37. 08. HANDLING UNCAUGHT EXCEPTIONS
  • 38. Uncaught Exceptions in Threads  Implement Thread.UncaughtExceptionHandler interface o Only one method: void uncaughtException(Thread t, Throwable e)  Three ways to use UncaughtExceptionHandler: o Set as default handler for all the threads in the system  static void Thread.setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler eh) o Set different handlers for different threads  void Thread.setUncaughtExceptionHandler(UncaughtExceptionHandler eh) o Use a combination of default-handler and thread-specific-handlers Leaking Exceptions from threads cannot be caught directly …
  • 39. Uncaught Exceptions in Executors  Implement Thread.UncaughtExceptionHandler interface o Only one method: void uncaughtException(Thread t, Throwable e)  Three ways to use UncaughtExceptionHandler: o Set as default handler for all the threads in the system  static void Thread.setDefaultUncaughtExceptionHandler(UncaughtExceptionHandler eh) o Set different handlers for different threads  Implement a ThreadFactory and;  Use void Thread.setUncaughtExceptionHandler(UncaughtExceptionHandler eh) o Use a combination of default-handler and thread-specific-handlers Similar ways of handling like the normal threads …
  • 40. 09. WAITING FOR OTHER THREADS TO COMPLETE
  • 41. Waiting for Normal-Threads to Finish  Previously discussed techniques: o boolean Thread.isAlive()  To be called multiple times till you get a ‘false’  Wastage of CPU cycles o wait() and notify() mechanism  To be coded manually in the tasks by the programmer  A little difficult to get right  Third technique: o void Thread.join()  In-built mechanism – works just like wait() and notify()  Easy to use Suspend the current thread so that it continues only after some other thread(s) have finished …
  • 42. Waiting for Executor Tasks to Finish  Use java.util.concurrent.CountDownLatch  An object of CountDownLatch is shared by the waiting tasks and the awaited tasks. o Waiting-tasks call void await() on the latch o Awaited-tasks call void countdown() on the latch after finishing their executions o When the count reaches zero, waiting tasks are released, thereby, bringing them out of suspended state and enabling their execution again. Suspend the current task so that it continues only after some other task(s) have finished …
  • 44. Scheduling Tasks using Threads-API  java.util.Timer o Spawns a thread for executing the tasks o Also contains scheduling logic  java.util.TimerTask o Implements Runnable interface o Represents the task o Should be short-lived for repeated executions For all scheduling needs, Threads-API provides two classes … void schedule(TimerTask task, Date time) void schedule(TimerTask task, long delay) void schedule(TimerTask task, Date firstTime, long period) void schedule(TimerTask task, long delay, long period) void scheduleAtFixedRate(TimerTask task, Date firstTime, long period) void scheduleAtFixedRate(TimerTask task, long delay, long period) void cancel() long scheduledExecutionTime() boolean cancel()
  • 45. The “java.util.Timer” Class  Single task-execution-thread per Timer object  Timer tasks should complete quickly  Always call Timer.cancel() when application is shutting down; otherwise it may cause memory leak  Don’t schedule any more tasks on the Timer after cancelling it  Timer class is thread-safe  Timer class does not offer any real-time guarantees Few things worth stressing upon about the Timer class …
  • 46. One-Time Execution in Future - Threads-API  Two Timer methods for scheduling one-time execution: o void schedule(TimerTask task, Date time) o void schedule(TimerTask task, long delay) Use Timer and TimerTask classes …
  • 47. Repeated Fixed-Delay Executions – Threads API Timer and TimerTask only, are used …  Two methods in Timer class: o void schedule(TimerTask task, long delay, long period) o void schedule(TimerTask task, Date firstTime, long period)  Next execution is scheduled when the current one begins  Each execution is schedule relative to the actual start time of the previous one  Start-times drift forward over long runs o Hence, frequency of executions becomes lower than that specified  Appropriate for activities that require ‘smoothness’ over short periods of time e.g. blinking cursor, hourly chimes, etc.
  • 48. Repeated Fixed-Rate Executions – Threads API Timer and TimerTask only, are used …  Two methods in Timer class: o void scheduleAtFixedRate(TimerTask task, long delay, long period) o void scheduleAtFixedRate(TimerTask task, Date firstTime, long period)  Next execution is scheduled when the current one begins  Each execution is schedule relative to the scheduled start time of the first execution  Start-times DO NOT drift forward over long runs o Hence, frequency of executions remains constant  Appropriate for activities that are sensitive to ‘absolute time’ and accurate frequency of executions
  • 49. Scheduling Tasks - Executors-API  Available pools: o Single-thread-scheduled-executor o Scheduled-thread-pool  Created using Executors (C)  ScheduledExecutorService (I) o Extends ExecutorService (I)  ScheduledFuture (I). Extends: o Future (I) o Delayed (I) Special thread-pool(s) provided by Executors for scheduling tasks … ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) <V> ScheduledFuture<V> schedule(Callable<V> callable, long delay, TimeUnit unit) ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit) ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit) static ScheduledExecutorService newSingleThreadScheduledExecutor() static ScheduledExecutorService newScheduledThreadPool(int corePoolSize) long getDelay(TimeUnit unit)
  • 50. One Time Execution in Future – Executors API ScheduledExecutorService is used …  Two methods: o ScheduledFuture<?> schedule(Runnable command, long delay, TimeUnit unit) o ScheduledFuture<?> schedule(Callable<V> callable, long delay, TimeUnit unit)  Executions can be scheduled using a single-thread-executor or scheduled-thread-pool  No need to extend TimerTask. Normal Runnables and Callables only are needed!
  • 51. Repeated Fixed-Delay Executions – Executors API ScheduledExecutorService is used …  Only one method: o ScheduledFuture<?> scheduleWithFixedDelay(Runnable command, long initialDelay, long delay, TimeUnit unit)  Executions can be scheduled using a single-thread-executor or scheduled-thread-pool  Each execution is schedule relative to the termination-time of the previous execution o Start-times drift forward over long runs o Hence, frequency of executions becomes lower than that specified  Only Runnables can be scheduled – no Callables!
  • 52. Repeated Fixed-Rate Executions – Executors API ScheduledExecutorService is used …  Only one method: o ScheduledFuture<?> scheduleAtFixedRate(Runnable command, long initialDelay, long period, TimeUnit unit)  Executions can be scheduled using a single-thread-executor or scheduled-thread-pool  Each execution is schedule relative to the scheduled start time of the first execution o Start-times DO NOT drift forward over long runs o Hence, frequency of executions remains constant  Only Runnables can be scheduled – no Callables!
  • 54. This deck is part of the full-fledged video course on : EFFICIENT JAVA MULTITHREADING WITH EXECUTORS Visit: Course