SlideShare a Scribd company logo
CONCURRENT PROGRAMMING
THREAD’S ADVANCED CONCEPTS
PROGRAMMAZIONE CONCORRENTE E DISTR.
Università degli Studi di Padova
Dipartimento di Matematica
Corso di Laurea in Informatica, A.A. 2015 – 2016
rcardin@math.unipd.it
Programmazione concorrente e distribuita
SUMMARY
 Callable tasks
 Futures
 Executors
 Executor services
 Deadlocks
2Riccardo Cardin
Programmazione concorrente e distribuita
CALLABLES
 A Callable is a Runnable, that returns a value
 The Callable type is parametrized on the type of its
return value
 Callable<Integer> represents an asynchronous
computation that will produce an Integer value
 The value computed is not directly available
 We need a type to represents a value that will be available in
the future...
 Represents a deferred computation
3Riccardo Cardin
public interface Callable<V> {
// The method can throw an exception,
// unlike Runnable run method
public V call() throws Exception;
}
Programmazione concorrente e distribuita
FUTURES
 A Future represents a computation whose
result will be available at some future time
 Start the computation, give someone the Future
object, and forget about it
 To obtain the result a synchronization is needed
 The get method blocks until the result is available
 Or until a timeout has been reached
4Riccardo Cardin
public interface Future<V> {
V get() throws . . .;
V get(long timeout, TimeUnit unit) throws . . .;
void cancel(boolean mayInterrupt);
boolean isCancelled();
boolean isDone();
}
Programmazione concorrente e distribuita
FUTURES
 Using FutureTask is possible to run a
Callable, obtaining a Future as result
 Adapter of the Runnable and Future interfaces
 Using FutureTask is possible to run a Callable using
a Thread
 Exception semantics
 ExecutionException: error during execution
 CancellationException: task was cancelled
5Riccardo Cardin
Callable<Integer> myComputation = . . .;
FutureTask<Integer> task = new FutureTask<Integer>(myComputation);
Thread t = new Thread(task); // it's a Runnable
t.start();
// . . .
Integer result = task.get(); // it's a Future
Programmazione concorrente e distribuita
FUTURES
6Riccardo Cardin
Programmazione concorrente e distribuita
FUTURES
 A Future have some interesting characteristics
 Immutable
 Once a future is completed, it will be completed forever
 Lets treat asynchronous programming in a
synchronous way
 Simplify the division of complex task into smaller ones, that
can be executed concurrently
7Riccardo Cardin
Future<Integer> future = /* Initialization */ ;
System.out.println(future.get()); // Blocks and print 42
System.out.println(future.get()); // Prints 42 again
produceSomething
startDoingSomething
doSomethingWithResult
r
Programmazione concorrente e distribuita
FUTURES
8Riccardo Cardin
Programmazione concorrente e distribuita
EXECUTORS
 Usually it doesn’t make sense to have a one-to-
one relationship between a task and a thread
 Thread is a mechanism for execution a sequence of
instructions (task)
 Creating a new thread means to ask some work to the OS, so
it is a time consuming operation
 When tasks are short lived, run many of them on the
same thread
 When tasks are computationally intensive, use one
thread per processor
 Avoid the overhead of context switching among threads
 Anyway, all that you need is a thread pool
9Riccardo Cardin
Programmazione concorrente e distribuita
EXECUTORS
 Executors are implementation of thread pools
 An homogeneous pool of worker threads
 Amortizes thread creation and teardown
 Improves responsiveness, due to lack of task execution’s delay
 Thread pools execution using static factory methods
 Each method return an executor instance that implements a
specific execution policy
10Riccardo Cardin
// Create the thread pool with a specified execution policy
Executor executor = Executors.newCachedThreadPool();
Runnable hellos = new Runnable() { /* Say hello a lot of times */ };
Runnable goodbyes =
new Runnable() {/* Say hello a lot of times */ };
// Submit task for execution to thread pool
executors.execute(hellos);
executors.execute(goodbyes);
Programmazione concorrente e distribuita
EXECUTORS
 An Executor executes tasks, choosing the
threads on which to run them
 You have not the full control on thread life cycle
 Based on the producer / consumer pattern
 Activities produce tasks, threads consume tasks
 Decoupling of task submission from task execution
 Simplier changing of execution policy
 What, where, when, and how of task execution
11Riccardo Cardin
Runnable task = new Runnable() { /* Some task */ };
Executor executor = // Get an instance to an executor
executor.execute(task); // A thread is choosen to execute the task
Programmazione concorrente e distribuita
EXECUTORS
 Execution policies
 Dependent on the available computing resources and
quality of service requirements
 In what thread will tasks be executed?
 In what order should tasks be executed (FIFO, LIFO, priority
order)?
 How many tasks may execute concurrently?
 How many tasks may be queued pending execution?
 If a task has to be rejected because the system is overloaded,
which task should be selected as the victim, and how should
the application be notified?
 What actions should be taken before or after executing a
task?
12Riccardo Cardin
Programmazione concorrente e distribuita
EXECUTORS
13Riccardo Cardin
 Available executors policies
Method Description
newCachedThreadPool New thread are created as needed; idle
threads are kept for 60 seconds
newFixedThreadPool The pool contains a fixed set of threads;
idle threads are kept indefinitely
newSingleThreadExecutor A «pool» with a single thread that executes
the submitted tasks sequentially (similar to
the Swing event dispatch thread)
newScheduledThreadPool A fixed-thread pool for scheduled execution
newSingleThreadScheduledExecutor A single-thread «pool» for scheduled
execution
Programmazione concorrente e distribuita
EXECUTORS
14Riccardo Cardin
Programmazione concorrente e distribuita
EXECUTOR SERVICES
 To execute a Callable, use an instance of the
ExecutorService interface
 Previous static factory methods return such instances
 1° submit returns a Future containing the task itself
 Control of execution (call isDone, cancel, isCancelled)
 2° submit acts like the first, but return result object
 3° submit returns a Future of the result of the Callable task
 Method invokeAll executes all the input Callables
15Riccardo Cardin
Future<?> submit(Runnable task)
Future<T> submit(Runnable task, T result)
Future<T> submit(Callable<T> task)
<T> List<Future<T>> invokeAll(Collection<? Extends Callable<T>>
task)
Tipicalworkflow
Programmazione concorrente e distribuita
EXECUTOR SERVICES
 Executor lifecycle
 Derived from the interface of ExecutorService type
16Riccardo Cardin
Running
Shutting
down
Terminated
Executors are created in running state.
In this state an executor accepts new tasks
and schedules them for execution
This state is reached after shutdown method
was called. No new task are accepted, but
previously submitted task are allowed to complete.
The shutdownNow method initiates an abrupt
shutdown: no queued task not yet begun is started
Once all task are completed, the executor
transitions to the terminated state
Programmazione concorrente e distribuita
DEADLOCKS
 Multiple threads wait forever due to a cyclic
locking dependency
 If threads are nodes and relations of dependency are
edges, a cyclic graph means to have a deadlock
 The JVM cannot detect deadlock, differently from
database systems
17Riccardo Cardin
A
R1
B
R2
C
R3 Also known as deadly embrace:
A needs a resource R hold by B, B
a resource hold by C, an so on...
Programmazione concorrente e distribuita
DEADLOCKS
 Deadlocks rarely manifest themeselves
immediatly (only in production under heavy load)
 Four conditions have to hold simultaneously (Coffman
conditions)
 Mutual exclusion: at least one resource must be held in a non-
shareable mode
 Hold and wait: a process is currently holding at least one
resource and requesting additional resources
 No preemption: a resource can be released only voluntarily
 Circular wait: a process must be waiting for a resource which is
being held by another process, which in turn is waiting for the
first process to release the resource
18Riccardo Cardin
Programmazione concorrente e distribuita
DEADLOCKS
19Riccardo Cardin
Programmazione concorrente e distribuita
EXAMPLES
20Riccardo Cardin
https://github.com/rcardin/pcd-snippets
Programmazione concorrente e distribuita
REFERENCES
 Chap. 14 «Multithreading», Core Java Volume I - Fundamentals, Cay
Horstmann, Gary Cornell, 2012, Prentice Hall
 Chap. 6 «Task Execution», Java Concurrency in Practice, Brian
Goetz, 2006, Addison-Wesley Professional
 Chap. 10 «Avoiding Liveness Hazards», Java Concurrency in
Practice, Brian Goetz, 2006, Addison-Wesley Professional
 Chap. 10 «Concurrent Programming», Core Java for the Impatient,
Cay Horstmann, 2015, Addison-Wesley
 Deadlocks https://en.wikipedia.org/wiki/Deadlock
21Riccardo Cardin

More Related Content

What's hot (20)

PPT
Introduction to-vhdl
Neeraj Gupta
 
PDF
System Verilog Functional Coverage
rraimi
 
PPTX
Java - Sockets
Riccardo Cardin
 
PPTX
GCC RTL and Machine Description
Priyatham Bollimpalli
 
PPT
Unit I Advanced Java Programming Course
parveen837153
 
PDF
Syntutic
Rohit Chintu
 
PPT
Strategy and Template Pattern
Jonathan Simon
 
PPT
SystemVerilog OOP Ovm Features Summary
Amal Khailtash
 
PDF
Lecture1
karim_ibrahim
 
PDF
Detecting aspect-specific code smells using Ekeko for AspectJ
Coen De Roover
 
PPTX
System verilog coverage
Pushpa Yakkala
 
PDF
Ekeko Technology Showdown at SoTeSoLa 2012
Coen De Roover
 
PDF
Advanced Reflection in Java
kim.mens
 
PDF
The SOUL Tool Suite for Querying Programs in Symbiosis with Eclipse
Coen De Roover
 
PDF
Doulos coverage-tips-tricks
Obsidian Software
 
PPT
10 strategy pattern
Abhijit Gaikwad
 
PPT
Behavioral modeling
dennis gookyi
 
PDF
Extending and scripting PDT
William Candillon
 
PDF
Tesseract. Recognizing Errors in Recognition Software
Andrey Karpov
 
Introduction to-vhdl
Neeraj Gupta
 
System Verilog Functional Coverage
rraimi
 
Java - Sockets
Riccardo Cardin
 
GCC RTL and Machine Description
Priyatham Bollimpalli
 
Unit I Advanced Java Programming Course
parveen837153
 
Syntutic
Rohit Chintu
 
Strategy and Template Pattern
Jonathan Simon
 
SystemVerilog OOP Ovm Features Summary
Amal Khailtash
 
Lecture1
karim_ibrahim
 
Detecting aspect-specific code smells using Ekeko for AspectJ
Coen De Roover
 
System verilog coverage
Pushpa Yakkala
 
Ekeko Technology Showdown at SoTeSoLa 2012
Coen De Roover
 
Advanced Reflection in Java
kim.mens
 
The SOUL Tool Suite for Querying Programs in Symbiosis with Eclipse
Coen De Roover
 
Doulos coverage-tips-tricks
Obsidian Software
 
10 strategy pattern
Abhijit Gaikwad
 
Behavioral modeling
dennis gookyi
 
Extending and scripting PDT
William Candillon
 
Tesseract. Recognizing Errors in Recognition Software
Andrey Karpov
 

Viewers also liked (15)

PDF
Presto updates to 0.178
Kai Sasaki
 
PPTX
Design pattern architetturali Model View Controller, MVP e MVVM
Riccardo Cardin
 
PPTX
Java - Processing input and output
Riccardo Cardin
 
PPTX
Design Pattern Strutturali
Riccardo Cardin
 
PPTX
Java Graphics Programming
Riccardo Cardin
 
PPTX
Diagrammi di Sequenza
Riccardo Cardin
 
PPTX
Errori comuni nei documenti di Analisi dei Requisiti
Riccardo Cardin
 
PPTX
Java - Collections framework
Riccardo Cardin
 
PPTX
Introduzione ai Design Pattern
Riccardo Cardin
 
PPTX
Diagrammi delle Classi
Riccardo Cardin
 
PPTX
SOLID - Principles of Object Oriented Design
Riccardo Cardin
 
KEY
Scala For Java Programmers
Enno Runne
 
PPTX
Software architecture patterns
Riccardo Cardin
 
PPTX
Design Pattern Architetturali - Dependency Injection
Riccardo Cardin
 
PPTX
Scala - the good, the bad and the very ugly
Bozhidar Bozhanov
 
Presto updates to 0.178
Kai Sasaki
 
Design pattern architetturali Model View Controller, MVP e MVVM
Riccardo Cardin
 
Java - Processing input and output
Riccardo Cardin
 
Design Pattern Strutturali
Riccardo Cardin
 
Java Graphics Programming
Riccardo Cardin
 
Diagrammi di Sequenza
Riccardo Cardin
 
Errori comuni nei documenti di Analisi dei Requisiti
Riccardo Cardin
 
Java - Collections framework
Riccardo Cardin
 
Introduzione ai Design Pattern
Riccardo Cardin
 
Diagrammi delle Classi
Riccardo Cardin
 
SOLID - Principles of Object Oriented Design
Riccardo Cardin
 
Scala For Java Programmers
Enno Runne
 
Software architecture patterns
Riccardo Cardin
 
Design Pattern Architetturali - Dependency Injection
Riccardo Cardin
 
Scala - the good, the bad and the very ugly
Bozhidar Bozhanov
 
Ad

Similar to Java - Concurrent programming - Thread's advanced concepts (20)

PPTX
Lecture 23-24.pptx
talha ijaz
 
PDF
Java concurrency
Abhijit Gaikwad
 
ODP
Concurrent Programming in Java
Ruben Inoto Soto
 
PPTX
Multi threading
gndu
 
PPT
Threads in Java
Gaurav Aggarwal
 
PPTX
Multi Threading
Ferdin Joe John Joseph PhD
 
PPT
cs2110Concurrency1.ppt
narendra551069
 
ODP
Java Concurrency, Memory Model, and Trends
Carol McDonald
 
PPTX
MULTITHREADING PROGRAMMING AND I/O THREAD
mohanrajm63
 
PPTX
Java-7 Concurrency
Masudul Haque
 
PDF
Asynchronous programming with Java & Spring
Ravindra Ranwala
 
PDF
Java Multithreading Using Executors Framework
Arun Mehra
 
PDF
Multithreading Introduction and Lifecyle of thread
Kartik Dube
 
PPT
Md09 multithreading
Rakesh Madugula
 
ODP
Multithreading 101
Tim Penhey
 
PPT
cs4240-multithreading.ppt presentation on multi threading
ShrutiPanda12
 
PPT
Threads
Nilesh Jha
 
PPT
9.multi-threading latest(MB).ppt .
happycocoman
 
PPTX
OS Module-2.pptx
bleh23
 
PPT
Threads in java, Multitasking and Multithreading
ssusere538f7
 
Lecture 23-24.pptx
talha ijaz
 
Java concurrency
Abhijit Gaikwad
 
Concurrent Programming in Java
Ruben Inoto Soto
 
Multi threading
gndu
 
Threads in Java
Gaurav Aggarwal
 
cs2110Concurrency1.ppt
narendra551069
 
Java Concurrency, Memory Model, and Trends
Carol McDonald
 
MULTITHREADING PROGRAMMING AND I/O THREAD
mohanrajm63
 
Java-7 Concurrency
Masudul Haque
 
Asynchronous programming with Java & Spring
Ravindra Ranwala
 
Java Multithreading Using Executors Framework
Arun Mehra
 
Multithreading Introduction and Lifecyle of thread
Kartik Dube
 
Md09 multithreading
Rakesh Madugula
 
Multithreading 101
Tim Penhey
 
cs4240-multithreading.ppt presentation on multi threading
ShrutiPanda12
 
Threads
Nilesh Jha
 
9.multi-threading latest(MB).ppt .
happycocoman
 
OS Module-2.pptx
bleh23
 
Threads in java, Multitasking and Multithreading
ssusere538f7
 
Ad

More from Riccardo Cardin (7)

PPTX
Design Pattern Comportamentali
Riccardo Cardin
 
PPTX
Design Pattern Creazionali
Riccardo Cardin
 
PPTX
Diagrammi di Attività
Riccardo Cardin
 
PPTX
Diagrammi Use Case
Riccardo Cardin
 
PPTX
Introduzione a UML
Riccardo Cardin
 
PPTX
Mvc e di spring e angular js
Riccardo Cardin
 
PPTX
Reactive programming principles
Riccardo Cardin
 
Design Pattern Comportamentali
Riccardo Cardin
 
Design Pattern Creazionali
Riccardo Cardin
 
Diagrammi di Attività
Riccardo Cardin
 
Diagrammi Use Case
Riccardo Cardin
 
Introduzione a UML
Riccardo Cardin
 
Mvc e di spring e angular js
Riccardo Cardin
 
Reactive programming principles
Riccardo Cardin
 

Recently uploaded (20)

PDF
>Nitro Pro Crack 14.36.1.0 + Keygen Free Download [Latest]
utfefguu
 
PPTX
Android Notifications-A Guide to User-Facing Alerts in Android .pptx
Nabin Dhakal
 
PPTX
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
 
PDF
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
 
PDF
IDM Crack with Internet Download Manager 6.42 Build 41
utfefguu
 
PDF
TEASMA: A Practical Methodology for Test Adequacy Assessment of Deep Neural N...
Lionel Briand
 
PPTX
ERP - FICO Presentation BY BSL BOKARO STEEL LIMITED.pptx
ravisranjan
 
PDF
What Is an Internal Quality Audit and Why It Matters for Your QMS
BizPortals365
 
PDF
Power BI vs Tableau vs Looker - Which BI Tool is Right for You?
MagnusMinds IT Solution LLP
 
PPTX
Automatic_Iperf_Log_Result_Excel_visual_v2.pptx
Chen-Chih Lee
 
PDF
Continouous failure - Why do we make our lives hard?
Papp Krisztián
 
PDF
Cloud computing Lec 02 - virtualization.pdf
asokawennawatte
 
PDF
WholeClear Split vCard Software for Split large vCard file
markwillsonmw004
 
PPTX
Iobit Driver Booster Pro 12 Crack Free Download
chaudhryakashoo065
 
PDF
AWS Consulting Services: Empowering Digital Transformation with Nlineaxis
Nlineaxis IT Solutions Pvt Ltd
 
PPTX
How Can Recruitment Management Software Improve Hiring Efficiency?
HireME
 
PDF
Writing Maintainable Playwright Tests with Ease
Shubham Joshi
 
PDF
Laboratory Workflows Digitalized and live in 90 days with Scifeon´s SAPPA P...
info969686
 
PDF
Telemedicine App Development_ Key Factors to Consider for Your Healthcare Ven...
Mobilityinfotech
 
PDF
Dealing with JSON in the relational world
Andres Almiray
 
>Nitro Pro Crack 14.36.1.0 + Keygen Free Download [Latest]
utfefguu
 
Android Notifications-A Guide to User-Facing Alerts in Android .pptx
Nabin Dhakal
 
Quality on Autopilot: Scaling Testing in Uyuni
Oscar Barrios Torrero
 
Designing Accessible Content Blocks (1).pdf
jaclynmennie1
 
IDM Crack with Internet Download Manager 6.42 Build 41
utfefguu
 
TEASMA: A Practical Methodology for Test Adequacy Assessment of Deep Neural N...
Lionel Briand
 
ERP - FICO Presentation BY BSL BOKARO STEEL LIMITED.pptx
ravisranjan
 
What Is an Internal Quality Audit and Why It Matters for Your QMS
BizPortals365
 
Power BI vs Tableau vs Looker - Which BI Tool is Right for You?
MagnusMinds IT Solution LLP
 
Automatic_Iperf_Log_Result_Excel_visual_v2.pptx
Chen-Chih Lee
 
Continouous failure - Why do we make our lives hard?
Papp Krisztián
 
Cloud computing Lec 02 - virtualization.pdf
asokawennawatte
 
WholeClear Split vCard Software for Split large vCard file
markwillsonmw004
 
Iobit Driver Booster Pro 12 Crack Free Download
chaudhryakashoo065
 
AWS Consulting Services: Empowering Digital Transformation with Nlineaxis
Nlineaxis IT Solutions Pvt Ltd
 
How Can Recruitment Management Software Improve Hiring Efficiency?
HireME
 
Writing Maintainable Playwright Tests with Ease
Shubham Joshi
 
Laboratory Workflows Digitalized and live in 90 days with Scifeon´s SAPPA P...
info969686
 
Telemedicine App Development_ Key Factors to Consider for Your Healthcare Ven...
Mobilityinfotech
 
Dealing with JSON in the relational world
Andres Almiray
 

Java - Concurrent programming - Thread's advanced concepts

  • 1. CONCURRENT PROGRAMMING THREAD’S ADVANCED CONCEPTS PROGRAMMAZIONE CONCORRENTE E DISTR. Università degli Studi di Padova Dipartimento di Matematica Corso di Laurea in Informatica, A.A. 2015 – 2016 [email protected]
  • 2. Programmazione concorrente e distribuita SUMMARY  Callable tasks  Futures  Executors  Executor services  Deadlocks 2Riccardo Cardin
  • 3. Programmazione concorrente e distribuita CALLABLES  A Callable is a Runnable, that returns a value  The Callable type is parametrized on the type of its return value  Callable<Integer> represents an asynchronous computation that will produce an Integer value  The value computed is not directly available  We need a type to represents a value that will be available in the future...  Represents a deferred computation 3Riccardo Cardin public interface Callable<V> { // The method can throw an exception, // unlike Runnable run method public V call() throws Exception; }
  • 4. Programmazione concorrente e distribuita FUTURES  A Future represents a computation whose result will be available at some future time  Start the computation, give someone the Future object, and forget about it  To obtain the result a synchronization is needed  The get method blocks until the result is available  Or until a timeout has been reached 4Riccardo Cardin public interface Future<V> { V get() throws . . .; V get(long timeout, TimeUnit unit) throws . . .; void cancel(boolean mayInterrupt); boolean isCancelled(); boolean isDone(); }
  • 5. Programmazione concorrente e distribuita FUTURES  Using FutureTask is possible to run a Callable, obtaining a Future as result  Adapter of the Runnable and Future interfaces  Using FutureTask is possible to run a Callable using a Thread  Exception semantics  ExecutionException: error during execution  CancellationException: task was cancelled 5Riccardo Cardin Callable<Integer> myComputation = . . .; FutureTask<Integer> task = new FutureTask<Integer>(myComputation); Thread t = new Thread(task); // it's a Runnable t.start(); // . . . Integer result = task.get(); // it's a Future
  • 6. Programmazione concorrente e distribuita FUTURES 6Riccardo Cardin
  • 7. Programmazione concorrente e distribuita FUTURES  A Future have some interesting characteristics  Immutable  Once a future is completed, it will be completed forever  Lets treat asynchronous programming in a synchronous way  Simplify the division of complex task into smaller ones, that can be executed concurrently 7Riccardo Cardin Future<Integer> future = /* Initialization */ ; System.out.println(future.get()); // Blocks and print 42 System.out.println(future.get()); // Prints 42 again produceSomething startDoingSomething doSomethingWithResult r
  • 8. Programmazione concorrente e distribuita FUTURES 8Riccardo Cardin
  • 9. Programmazione concorrente e distribuita EXECUTORS  Usually it doesn’t make sense to have a one-to- one relationship between a task and a thread  Thread is a mechanism for execution a sequence of instructions (task)  Creating a new thread means to ask some work to the OS, so it is a time consuming operation  When tasks are short lived, run many of them on the same thread  When tasks are computationally intensive, use one thread per processor  Avoid the overhead of context switching among threads  Anyway, all that you need is a thread pool 9Riccardo Cardin
  • 10. Programmazione concorrente e distribuita EXECUTORS  Executors are implementation of thread pools  An homogeneous pool of worker threads  Amortizes thread creation and teardown  Improves responsiveness, due to lack of task execution’s delay  Thread pools execution using static factory methods  Each method return an executor instance that implements a specific execution policy 10Riccardo Cardin // Create the thread pool with a specified execution policy Executor executor = Executors.newCachedThreadPool(); Runnable hellos = new Runnable() { /* Say hello a lot of times */ }; Runnable goodbyes = new Runnable() {/* Say hello a lot of times */ }; // Submit task for execution to thread pool executors.execute(hellos); executors.execute(goodbyes);
  • 11. Programmazione concorrente e distribuita EXECUTORS  An Executor executes tasks, choosing the threads on which to run them  You have not the full control on thread life cycle  Based on the producer / consumer pattern  Activities produce tasks, threads consume tasks  Decoupling of task submission from task execution  Simplier changing of execution policy  What, where, when, and how of task execution 11Riccardo Cardin Runnable task = new Runnable() { /* Some task */ }; Executor executor = // Get an instance to an executor executor.execute(task); // A thread is choosen to execute the task
  • 12. Programmazione concorrente e distribuita EXECUTORS  Execution policies  Dependent on the available computing resources and quality of service requirements  In what thread will tasks be executed?  In what order should tasks be executed (FIFO, LIFO, priority order)?  How many tasks may execute concurrently?  How many tasks may be queued pending execution?  If a task has to be rejected because the system is overloaded, which task should be selected as the victim, and how should the application be notified?  What actions should be taken before or after executing a task? 12Riccardo Cardin
  • 13. Programmazione concorrente e distribuita EXECUTORS 13Riccardo Cardin  Available executors policies Method Description newCachedThreadPool New thread are created as needed; idle threads are kept for 60 seconds newFixedThreadPool The pool contains a fixed set of threads; idle threads are kept indefinitely newSingleThreadExecutor A «pool» with a single thread that executes the submitted tasks sequentially (similar to the Swing event dispatch thread) newScheduledThreadPool A fixed-thread pool for scheduled execution newSingleThreadScheduledExecutor A single-thread «pool» for scheduled execution
  • 14. Programmazione concorrente e distribuita EXECUTORS 14Riccardo Cardin
  • 15. Programmazione concorrente e distribuita EXECUTOR SERVICES  To execute a Callable, use an instance of the ExecutorService interface  Previous static factory methods return such instances  1° submit returns a Future containing the task itself  Control of execution (call isDone, cancel, isCancelled)  2° submit acts like the first, but return result object  3° submit returns a Future of the result of the Callable task  Method invokeAll executes all the input Callables 15Riccardo Cardin Future<?> submit(Runnable task) Future<T> submit(Runnable task, T result) Future<T> submit(Callable<T> task) <T> List<Future<T>> invokeAll(Collection<? Extends Callable<T>> task) Tipicalworkflow
  • 16. Programmazione concorrente e distribuita EXECUTOR SERVICES  Executor lifecycle  Derived from the interface of ExecutorService type 16Riccardo Cardin Running Shutting down Terminated Executors are created in running state. In this state an executor accepts new tasks and schedules them for execution This state is reached after shutdown method was called. No new task are accepted, but previously submitted task are allowed to complete. The shutdownNow method initiates an abrupt shutdown: no queued task not yet begun is started Once all task are completed, the executor transitions to the terminated state
  • 17. Programmazione concorrente e distribuita DEADLOCKS  Multiple threads wait forever due to a cyclic locking dependency  If threads are nodes and relations of dependency are edges, a cyclic graph means to have a deadlock  The JVM cannot detect deadlock, differently from database systems 17Riccardo Cardin A R1 B R2 C R3 Also known as deadly embrace: A needs a resource R hold by B, B a resource hold by C, an so on...
  • 18. Programmazione concorrente e distribuita DEADLOCKS  Deadlocks rarely manifest themeselves immediatly (only in production under heavy load)  Four conditions have to hold simultaneously (Coffman conditions)  Mutual exclusion: at least one resource must be held in a non- shareable mode  Hold and wait: a process is currently holding at least one resource and requesting additional resources  No preemption: a resource can be released only voluntarily  Circular wait: a process must be waiting for a resource which is being held by another process, which in turn is waiting for the first process to release the resource 18Riccardo Cardin
  • 19. Programmazione concorrente e distribuita DEADLOCKS 19Riccardo Cardin
  • 20. Programmazione concorrente e distribuita EXAMPLES 20Riccardo Cardin https://github.com/rcardin/pcd-snippets
  • 21. Programmazione concorrente e distribuita REFERENCES  Chap. 14 «Multithreading», Core Java Volume I - Fundamentals, Cay Horstmann, Gary Cornell, 2012, Prentice Hall  Chap. 6 «Task Execution», Java Concurrency in Practice, Brian Goetz, 2006, Addison-Wesley Professional  Chap. 10 «Avoiding Liveness Hazards», Java Concurrency in Practice, Brian Goetz, 2006, Addison-Wesley Professional  Chap. 10 «Concurrent Programming», Core Java for the Impatient, Cay Horstmann, 2015, Addison-Wesley  Deadlocks https://en.wikipedia.org/wiki/Deadlock 21Riccardo Cardin