我有一个从线程扩展的类:
public class Generador extends Thread {
...
public Generador(int marca, float sleep) {
this.marca = marca;
this.sleep = sleep;
this.cantActivaciones = 0;
}
@Override
public void start() {
marcadorAct++;
cantActivaciones++;
if (marcadorAct == 3) {
desalojarPasajero = true;
marcadorAct = 0;
} else {
desalojarPasajero = false;
}
}
@Override
public void run() {
System.out.println("The thread is running.....");
}
我的问题是,当我使用 start() 方法时,run() 方法不起作用。这是我的代码:
public class Simulación {
...
Generador generador[] = new Generador[30]; // Arr of threads...
public Simulación() { // The constructor
....
for (int i = 0; i < generador.length; i++) {
generador[i] = new Generador((i + 1) * 1000000, rand.nextInt(3) + rand.nextFloat());
}
....
}
public void generarPasajero() { // The method who decides to start a thread...
for (int i = 0; i < generador.length; i++) {
if (!generador[i].isAlive()) {
generador[i].start(); // HERE START DOESNT RUN THE RUN() METHOD
break;
}
}
}
希望你能理解我的问题.. 任何人都可以帮我解决这个问题吗?谢谢!
最佳答案
通过重写 start()
方法,您可以防止该方法本身执行的操作,因为您替换了它执行的代码。这意味着它不会通过运行 run()
方法来启动线程。
关于java - 当我在java中启动线程时,run()方法没有运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58453437/