Bonjour,
Je voudrais de l'aide sur l'utilisation d'un SingleThreadExecutor dans le cas suivant :
En sortie, je voudrais obtenir:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 public class Ordonanceur { private ExecutorService e = Executors.newSingleThreadExecutor(); public static void main(String[] args) { new Ordonanceur().tache(); } public void tache() { Future<?> submit = e.submit(new Runnable() { @Override public void run() { System.out.println("begin tache"); tache1(); tache2(); System.out.println("end tache"); } }); } public void tache1() { Future<?> submit = e.submit(new Runnable() { @Override public void run() { System.out.println("tache 1"); } }); try { submit.get(); } catch (InterruptedException | ExecutionException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } public void tache2() { Future<?> submit = e.submit(new Runnable() { @Override public void run() { System.out.println("tache 2"); } }); try { submit.get(); } catch (InterruptedException | ExecutionException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } }
Sauf qu'évidement j'ai un deadlock la methode tache1() attend tache() et vice versa.begin tache
tache 1
tache 2
end tache
Dans mon cas j'ai besoin d'avoir ce code tel quel, tache1() et tache 2() peuvent être appelées indépendamment de tache() et leur code doit être exécuté par l'executor.
Je pensais à faire faire un wrapper autour de l'executor et dans le code de la methode submit un truc du genre :
je voulais votre avis sur ma solution et éventuellement d'autre solution , je ne doit pas être le premier à avoir ce besoin
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8 //EXECUTOR_THREAD_ID est récupérer par une première tache qui sert uniquement à ça. if(Thread.currentThread().getId() == EXECUTOR_THREAD_ID){ runnable.run(); } else{ executor.submit(runnable); }
Partager