Professional Documents
Culture Documents
Franoise Baude
Plutt se baser sur la notion de pool (rservoir) de threads et affecter dynamiquement chaque tache une thread libre (par le biais dun executor )
Trop de taches bloques => plus, ou presque plus de threads disponibles ( thread leakage ) Compromis habituel entre gain et overheads!
Beaucoup de taches : stockage important pour taches en attente (versus une thread / tache) Trouver la bonne taille pour le pool de threads: fonctions du nombre de cores, types de taches (I/O-bound ou CPUbound)
Une implmentation d Executor est libre de choisir comment grer laffectation des taches aux threads
The Executor interface does not strictly require that execution be asynchronous. In the simplest case, an executor can run the submitted task immediately in the caller's thread:
class DirectExecutor implements Executor { public void execute(Runnable r) { r.run(); } } execute( More typically, tasks are executed in some thread other than the caller's thread. The executor below spawns a new thread for each task: class ThreadPerTaskExecutor implements Executor { public void execute(Runnable r) { new Thread(r).start(); } } execute( Thread(r).start();
Callable : motivation
Lorsque une tache (qui sera excute par une thread) doit renvoyer un rsultat :
Au lieu detre Runnable Elle est Callable java.util.concurrent Interface Callable<V> Type Parameters:
V - the result type of method call
Futures
Futures allow a client to continue in parallel with a host until the future value is needed:
Oscar Nierstrasz
11
Solution:
le rsultat de la tache est manipul par le biais dun Future, renvoy immdiatement lors de la soumission,
reprsente une promesse de rponse Permet de senqurir de lavancement de la tache, voire lannuler
ExecutorServices provide methods arranging asynchronous execution of any function expressed as Callable, the resultbearing analog of Runnable
<T> Future<T> submit(Callable<T> task) <T> T invokeAny(Collection<? extends Callable<T>> tasks) <T> List<Future<T>> invokeAll(Collection<? extends Callable<T>> tasks)
Future interface
Interface Future<V> Type Parameters:
java.util.concurrent V - The result type returned by this Future's get method
The result can only be retrieved using method get when the computation has completed, blocking if necessary until it is ready
to immediately block waiting for a task: result = exec.submit(aCallable).get();
Comment ?
Par une rification des appels de mthodes en Java Rajout de traitements Meta (Meta Object Protocol), dont RMI