0

我正在编写一个小游戏,其目标是管理商场。我的问题是动画。

实际上,我的同事创建了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?

4

2 回答 2

4

不要扩展线程。相反,我会使用 ExecutorService。您可以向它提交任意数量的 Runnables 任意次数。举些例子

Java 线程池教程

您创建一个执行器服务,例如

ExecutorService es = Executors.newSingleThreadExecutor();

您可以提交类似的任务

Runnable myRunnable = new MyRunnable();

es.submit(myRunnable);

并再次提交

es.submit(myRunnable);
于 2013-05-25T18:19:32.453 回答
1

!!!好的,它工作,问题是 int k。我必须在执行的操作按钮中将其设为 0 才能重新开始运行 谢谢大家!!!:D

于 2013-05-26T12:46:48.767 回答