Interruption et reprise d'un Thread
Voila, j'ai créé une classe qui lance, suspend et reprend un thread, via différentes méthodes.
Voici cette classe:
Code:
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
|
package evolution;
public class Vivre implements Runnable{
protected Thread t;
protected boolean encours,demarre;
protected Interface IHM=null;
public Vivre(Interface i){
encours=false;
demarre=false;
t=new Thread(this);
IHM=i;
}
public void Demarrer(){
t.start();
encours=true;
}
public void Reprendre(){
t.notify();
encours=true;
}
public void Suspendre() throws InterruptedException{
t.wait();
encours=false;
}
public synchronized void run() {
if(!demarre){
demarre=true;
while(IHM.grille.nbAmibe!=1){
try {
Interface.sortie.append(IHM.grille.vivre());
Thread.sleep(1500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
IHM.affichageInfo();
}
}
}
} |
Je manipule un objet Vivre dans le action performed de mon IHM :
Code:
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
|
public class Interface extends ... implements ...{
protected Vivre v;
public Interface(){
v=new Vivre(this);
[...]
}
public void actionPerformed(ActionEvent e) {
Object o = e.getSource();
/*
* Action sur le bouton pour faire vivre la population
*/
if(o==boutonVie){
if(!v.encours){
if(!v.demarre) v.Demarrer();
else {
v.Reprendre();
}
}
else
try {
v.Suspendre();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
[...]
}
} |
En l'état, lorsque je clique sur mon bouton Vivre, cela me lance bien le thread, et l'affichage de la grille avec des pauses de 1500ms.
Mais il y a 2 problèmes :
- lorsque j'utilisais la méthode de vie de la population, je passais directement de l'affichage de l'ancienne grille à la nouvelle grille. Or maintenant, l'affichage se fait "en direct", c'est à dire que je vois s'afficher chaque une par une, sachant que plus il y a de cases, et plus les dimensions de chacunes se réduisent, bref, c'est moche, et surtout je ne comprend pas pourquoi. Comment l'éviter???
- le vrai problème, c'est qu'un nouveau clic sur le bouton vie ne produit rien, si ce n'est qu'une exception est levée :
Citation:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalMonitorStateException: current thread not owner
at java.lang.Object.wait(Native Method)
at java.lang.Object.wait(Unknown Source)
at evolution.Vivre.Suspendre(Vivre.java:28)
at evolution.Interface.actionPerformed(Interface.java:1302)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Pourquoi???