Bonjour,
J'utilise les méthodes wait et notify en appelant wait() dans le run d'un thread, et en appelant notify dans le run d'un autre thread sur l'objet thread qui est en attente.
Mais est-il possible de lancer ce notify ailleurs que dans un autre thread ?
Par exemple dans une méthode appelée dans le run avant le wait (dans mon exemple maMethode). Ou bien est-ce que la méthode ne fera pas son traitement du fait du wait?
Ce qui donnerai l'exemple suivant :
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 public class monThread extends Thread { int[] monTableau; ArrayList<Integer> suivantsDe0 = new ArrayList<Integer>(); public void run() { while(!isInterrupted()) { if(monTableau[0] == 0){ maMethode(); try{ wait(); } catch(InterruptedException e){} } else{ for(int i=0; i<monTableau.length-1 ; i++) monTableau[i] = monTableau[i+1]; monTableau[monTableau.length] = 0; } } } private void maMethode(){ if(monTableau.length > 1){ suivantsDe0.add(monTableau[1]); for(int i=0; i<monTableau.length-2 ; i++) monTableau[i] = monTableau[i+2]; monTableau[monTableau.length-1] = 0; monTableau[monTableau.length] = 0; } notify(); } }
Partager