gpt4 book ai didi

java - 线程 java - 可以重新初始化吗?

转载 作者:行者123 更新时间:2023-12-01 17:23:36 25 4
gpt4 key购买 nike

我正在编写一个小游戏,其目标是管理一个购物中心。我的问题是动画。

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

最佳答案

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

Java Thread Pools tutorial

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

ExecutorService es = Executors.newSingleThreadExecutor();

您可以提交类似的任务

Runnable myRunnable = new MyRunnable();

es.submit(myRunnable);

并再次提交

es.submit(myRunnable);

关于java - 线程 java - 可以重新初始化吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16752553/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com