java - concurrent programming - thread's advanced concepts
TRANSCRIPT
CONCURRENT PROGRAMMINGTHREAD’S ADVANCED CONCEPTSPROGRAMMAZIONE CONCORRENTE E DISTR.Università degli Studi di Padova
Dipartimento di Matematica
Corso di Laurea in Informatica, A.A. 2015 – [email protected]
2Programmazione concorrente e distribuita
SUMMARY Callable tasks Futures Executors Executor services Deadlocks
Riccardo Cardin
3Programmazione 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
Riccardo Cardin
public interface Callable<V> { // The method can throw an exception, // unlike Runnable run method public V call() throws Exception;}
4Programmazione concorrente e distribuita
FUTURES A Future represents a computation whose result
will be available at some future timeStart 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
Riccardo 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 resultAdapter of the Runnable and Future interfacesUsing 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 Runnablet.start();// . . .Integer result = task.get(); // it's a Future
6Programmazione concorrente e distribuita
FUTURES
Riccardo Cardin
7Programmazione 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
Riccardo Cardin
Future<Integer> future = /* Initialization */ ;System.out.println(future.get()); // Blocks and print 42System.out.println(future.get()); // Prints 42 again
produceSomething
startDoingSomething
doSomethingWithResult
r
8Programmazione concorrente e distribuita
FUTURES
Riccardo Cardin
9Programmazione concorrente e distribuita
EXECUTORS Usually it doesn’t make sense to have a one-to-
one relationship between a task and a threadThread 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 operationWhen tasks are short lived, run many of them on the
same threadWhen 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
Riccardo Cardin
10Programmazione concorrente e distribuita
EXECUTORS Executors are implementation of thread pools
An homogeneous pool of worker threadsAmortizes thread creation and teardown
Improves responsiveness, due to lack of task execution’s delayThread pools execution using static factory methods
Each method return an executor instance that implements a specific execution policy
Riccardo Cardin
// Create the thread pool with a specified execution policyExecutor 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 poolexecutors.execute(hellos);executors.execute(goodbyes);
11Programmazione concorrente e distribuita
EXECUTORS An Executor executes tasks, choosing the
threads on which to run themYou 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 executionSimplier changing of execution policy
What, where, when, and how of task execution
Riccardo Cardin
Runnable task = new Runnable() { /* Some task */ };Executor executor = // Get an instance to an executorexecutor.execute(task); // A thread is choosen to execute the task
12Programmazione 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?
Riccardo Cardin
13Programmazione concorrente e distribuita
EXECUTORS
Riccardo Cardin
Available executors policies
Method DescriptionnewCachedThreadPool New thread are created as needed;
idle threads are kept for 60 secondsnewFixedThreadPool 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
14Programmazione concorrente e distribuita
EXECUTORS
Riccardo Cardin
15Programmazione concorrente e distribuita
EXECUTOR SERVICES To execute a Callable, use an instance of the ExecutorService interfacePrevious 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
Riccardo 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)
Tipi
cal w
orkfl
ow
16Programmazione concorrente e distribuita
EXECUTOR SERVICES Executor lifecycle
Derived from the interface of ExecutorService type
Riccardo Cardin
Running
Shutting down
Terminated
Executors are created in running state. In this state an executor accepts new tasksand schedules them for executionThis 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
17Programmazione 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 Riccardo Cardin
AR1
BR2
C
R3 Also known as deadly embrace:A needs a resource R hold by B, B a resource hold by C, an so on...
18Programmazione 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
Riccardo Cardin
19Programmazione concorrente e distribuita
DEADLOCKS
Riccardo Cardin
20Programmazione concorrente e distribuita
EXAMPLES
Riccardo Cardin
https://github.com/rcardin/pcd-snippets
21Programmazione 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
Riccardo Cardin