我正在编写一个小游戏,其目标是管理商场。我的问题是动画。
实际上,我的同事创建了Animateur
扩展Thread
和实现的类Runnable
。
在我的框架中,我声明并初始化了这个类,按下一个按钮,我需要他run()
执行。
动画由来自商场一侧的人组成,他们买了东西然后从另一侧离开。
要开始一天,我需要调用该run()
方法。
第一天,推后,一切都很完美:我看到了人和所有的人。但是当我第二次按下同一个按钮,开始新的一天时,一切正常,我被卡住了。我不能玩,因为我无法开始新的一天——run()
在Animateur
课堂上再执行一次。
有人可以帮助我或给我一些想法来解决这个问题吗?:(
package myMall;
import java.util.ArrayList;
import java.util.List;
import javax.swing.JOptionPane;
public class Animateur extends Thread implements Runnable {
private int giorno = 1;
private int giornoAttuale=1;
private int delay = 30;
private int nbVisiteurs =0;
private int k=0;
private double gainmoyen;
public MonMall m;
public List<Integer> w = new ArrayList<Integer>();
public List<Integer> e = new ArrayList<Integer>();
public int [] r = new int[6];
public Animateur(MonMall k) {
this.m = k;
m.setGainmoyen((int)this.gainmoyen);
m.setVisitestructure(this.r);
}
public void setAvance(){
this.giornoAttuale++;
}
public void run() {
while (m.isSimulation()) {
if(giorno<=giornoAttuale){
k = k + 30;
if (k < 10000) {
Client c = new Client(1000, 0, 0, 0);
m.listeTemps.add(k);
if (m.GenereClient(0.05)) {
m.Clients.add(c);
m.ajouterVisiteur(c);
//System.out.println(entreesec(c));
m.destination.add(m.entreesec(c));
nbVisiteurs++;
m.listeVisiteursentres.add(nbVisiteurs);
}else{w.add(nbVisiteurs);}
}
for (int i = 0; i < m.Clients.size(); i++) { //generaliser a Visiteurs
Client a = m.Clients.get(i);
a.move();
//System.out.println(m.getBudget());
if ((a.getLigne() == 10) && (a.getColonne() == 18)) {
m.Clients.remove(i);
m.Visiteurs.remove(a);
}
if (!m.destination.isEmpty()) {
Element b = m.destination.get(i);
if (a.getLigne() == b.getLigne() && a.getColonne() == b.getColonne() - 1) {
b.entreereelle(a);
m.setBudget(m.getBudget() + b.getGain());
this.gainmoyen+=m.getBudget();
//b.sortie(a);//una estructura nunca se llena
//System.out.println(m.getBudget());
} else if (a.getLigne() == b.getLigne() && a.getColonne() == b.getColonne() + 1) {
b.entreereelle(a);
m.setBudget(m.getBudget() + b.getGain());
//b.sortie(a);
//System.out.println(m.getBudget());
}
}
}
if (m.Clients.isEmpty() && k > 12000) {// Pb con el numer o de clientes entrados pero solucionable
m.setSimulation(false);
//m.setListeTemps(e);
//m.setListeVisiteursentres(w);
this.e= m.listeTemps;
this.w= m.listeVisiteursentres;
this.gainmoyen=this.gainmoyen/this.nbVisiteurs;
for(Element e: m.destination){
if(e instanceof Clinique ){
r[0]++;
}else if (e instanceof CommerceGeneral){
r[1]++;
}else if (e instanceof CommerceSpecifique){
r[2]++;
}else if (e instanceof Fun){
r[3]++;
}else if (e instanceof Restauration){
r[4]++;
}else if (e instanceof Gym){
r[5]++;
}
}
System.out.println(m.isSimulation());
(new JOptionPane()).showMessageDialog(null, "Journee finie", "Fin!", JOptionPane.INFORMATION_MESSAGE);
try {
Thread.sleep(this.delay);
}catch (InterruptedException e) {
}
}
//m.notifyFin(); //NO FUNCIONA¿?
}
giorno++;
}
}
}
我试图销毁创建的线程并在单击时初始化一个新线程,但它不起作用
编辑:
谢谢你的回答。要使用执行器服务,我只需要实现这个类来代替Runnable
?