Tema: Thread java
Ver Mensaje Individual
  #1 (permalink)  
Antiguo 03/03/2012, 05:10
Blashak
 
Fecha de Ingreso: noviembre-2007
Mensajes: 471
Antigüedad: 16 años, 5 meses
Puntos: 2
Thread java

Buenas,

Estoy haciendo un ejercicio para el cole en el que me piden que haga una carrera de coches y cada coche se estara manejando por hilos diferentes con prioridad de hilos aleatoria.
Al hacerlo me no me funciona bien el movimiento de los coches ta que solo se mueve un coche y el resto no.

codigo:



new Automovil("coche1");
new Automovil("coche2");
new Automovil("coche3");


class Automovil extends cars implements Runnable {

int i = 100;
int k = 0;

Thread t;
int sleept = 1000;
String car;
int num;

public Automovil(String car, int num){

this.car = car;
this.num = num;
t = new Thread(this);

t.setPriority(t.getPriority() + 1);

t.start();

}


public void run() {

while(k<i){
try {

t.sleep(sleept);
if(this.car.equals("coche1")){

Dimension size = imagen1.getPreferredSize();
imagen1.setBounds(k, 40,
size.width, size.height);

System.out.println(this.car + "Posicion " + k);


}else if(this.car.equals("coche2")){

Dimension size = imagen2.getPreferredSize();
imagen2.setBounds(k, 80,
size.width, size.height);

System.out.println(this.car + "Posicion " + k);

}else if(this.car.equals("coche3")){

Dimension size = imagen3.getPreferredSize();
imagen3.setBounds(k, 120,
size.width, size.height);

System.out.println(this.car + "Posicion " + k);

}


k++;
} catch (InterruptedException ex) {
Logger.getLogger(Automovil.class.getName()).log(Le vel.SEVERE, null, ex);
}

}


}


}


la imagen no se mueve del primer coche y segundo pero tercero si aunque el print de la posicion de cada coche si que se hace.

a que es debido ??

Saludos