Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Corredor implements Runnable {
- private double distancia;
- public Corredor(double distancia)
- {
- this.distancia = distancia;
- }
- @Override
- public void run() {
- update();
- }
- public void update()
- {
- double velocidad, aux = 0, tiempo = 0;
- try{
- while (aux < distancia)
- {
- synchronized(this){
- velocidad = velocidadPuntual(aux);
- Thread.sleep(2);
- tiempo +=2;
- aux += velocidad*2;
- System.out.println("nCorredor: "+Thread.currentThread().getName());
- System.out.println("Distancia recorrida: "+aux);
- System.out.println("Tiempo: "+tiempo+"n");
- if(aux > 100)
- break;
- }
- }
- }catch(InterruptedException e){}
- }
- private double velocidadPuntual(double aux)
- {
- double velocidad;
- if(aux < 10)
- velocidad = Math.random()*5+5;
- else if(aux >= 10 && aux < 20)
- velocidad = Math.random()*5 + 7;
- else if(aux >= 20 && aux < 30)
- velocidad = Math.random()*5 + 8;
- else if(aux >= 30 && aux < 60)
- velocidad = Math.random()*5 + 10;
- else if(aux >= 60 && aux < 80)
- velocidad = Math.random()*5 + 9;
- else
- velocidad = Math.random()*5 + 8;
- return velocidad;
- }
- }
- package practicasdhilos;
- import carrera.Corredor;
- /**
- *
- * @author ignacio
- */
- public class PracticaSDHIlos {
- /**
- * @param args the command line arguments
- */
- public static void main(String[] args) {
- // TODO code application logic here
- double prueba = 100;
- Thread Bolt = new Thread (new Corredor(prueba),"Bolt");
- Thread Aranguren = new Thread (new Corredor(prueba),"Aranguren");
- Thread Lewis = new Thread (new Corredor(prueba), "Lewis");
- Thread Powell = new Thread (new Corredor(prueba), "Powell");
- Thread Harris = new Thread (new Corredor(prueba), "Harris");
- Bolt.start();
- Aranguren.start();
- Lewis.start();
- Powell.start();
- Harris.start();
- }
- }
Add Comment
Please, Sign In to add comment