Bonjour,
Comment arrêter un thread lors l'activity isFinish ?
Merci.
Bonjour,
Comment arrêter un thread lors l'activity isFinish ?
Merci.
de manière propre:
Pour ton Thread:
Dans ton Activity:
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 public class MyRunnable extends Runnable { private boolean mContinue = true; public void run() { while(continue) { //TODO } } public void stop() { mContinued = false; } }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 public void onDestroy() { super.onDestroy(); mMyRunnable.stop(); mMyThread.join(); // bloque jusque à la mort du thread; }
J'ai fait ceci:
Dans mon Thread
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4 public synchronized void stop() { this.stopThread = true; }
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14 public void run() { boolean fin = false; while (!fin) { synchronized (this) { Thread.yield(); // lecture du boolean fin = MaClasse.this.stopThread; } traitement(); } }
Donc c'est bon?
Par contre ici; sauf erreur de ma part, tes synchronized sont inutiles.
J'ai trouvé la solution ici:
http://java.developpez.com/faq/java/...angage_threads
Partager