2013-05-25 22 views
0

我编码一个小游戏,其目标是管理一个商场。我的问题是动画。线程java - 可以重新初始化吗?

实际上,我的同事创建了类Animateur,它扩展了Thread并实现了Runnable

在我的框架中,我声明并初始化这个类,然后按下一个按钮,我需要他的run()执行。

动画由来自购物中心一侧的人组成,买东西离开另一边。

要开始,我需要调用run()方法。

推后的第一天,一切都是完美的:我看到人和所有人。但是,当我第二次按下同一个按钮,开始新的一天时,任何工作都会发生,我会陷入困境。 我无法玩,因为我无法开始新的一天 - 再次执行Animateur课程中的run()

有人可以帮我或给我一些想法来解决这个问题吗? :(

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++; 
     } 
    } 
} 

我试图摧毁创建的线程并初始化在点击一个新的,但奥斯卡最佳吨工作

编辑:

谢谢你的answer要使用执行者服务,我只需要实现这个类代替Runnable

+1

一旦执行'Thread'就不应该重新运行,而应该创建一个新的'Thread'对象并运行它。 –

+0

我们认识到代码的相关部分(除非你希望我们开始猜测:))。 – gkalpak

回答

4

不扩展线程,而是使用ExecutorService。你可以提交任意数量的Runnables给它任何n次数。对于例子

Java Thread Pools tutorial

你创建了

ExecutorService es = Executors.newSingleThreadExecutor(); 

一个执行的服务,您可以提交一个任务一样

Runnable myRunnable = new MyRunnable(); 

es.submit(myRunnable); 

,并再次提交

es.submit(myRunnable); 
+0

请问你能解释一下它是如何工作的吗?我试了一些东西,但似乎不正确 –

+0

我已经添加了一个例子。你尝试了什么,你的意思是“不正确”? –

+0

我假设你的意思是'Executors.newSingleThreadExecutor()',对吧? – Nate

1

!!!好吧,它的工作,问题是int k。 我不得不在动作执行按钮重新运行 谢谢大家!!! D