Guest User

Untitled

a guest
Nov 15th, 2018
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. public class Corredor implements Runnable {
  2. private double distancia;
  3.  
  4. public Corredor(double distancia)
  5. {
  6. this.distancia = distancia;
  7. }
  8. @Override
  9. public void run() {
  10. update();
  11. }
  12.  
  13. public void update()
  14. {
  15. double velocidad, aux = 0, tiempo = 0;
  16. try{
  17. while (aux < distancia)
  18. {
  19. synchronized(this){
  20. velocidad = velocidadPuntual(aux);
  21. Thread.sleep(2);
  22. tiempo +=2;
  23. aux += velocidad*2;
  24. System.out.println("nCorredor: "+Thread.currentThread().getName());
  25. System.out.println("Distancia recorrida: "+aux);
  26. System.out.println("Tiempo: "+tiempo+"n");
  27. if(aux > 100)
  28. break;
  29.  
  30. }
  31. }
  32.  
  33. }catch(InterruptedException e){}
  34.  
  35. }
  36.  
  37. private double velocidadPuntual(double aux)
  38. {
  39. double velocidad;
  40. if(aux < 10)
  41. velocidad = Math.random()*5+5;
  42. else if(aux >= 10 && aux < 20)
  43. velocidad = Math.random()*5 + 7;
  44. else if(aux >= 20 && aux < 30)
  45. velocidad = Math.random()*5 + 8;
  46. else if(aux >= 30 && aux < 60)
  47. velocidad = Math.random()*5 + 10;
  48. else if(aux >= 60 && aux < 80)
  49. velocidad = Math.random()*5 + 9;
  50. else
  51. velocidad = Math.random()*5 + 8;
  52.  
  53. return velocidad;
  54. }
  55.  
  56. }
  57.  
  58. package practicasdhilos;
  59. import carrera.Corredor;
  60.  
  61. /**
  62. *
  63. * @author ignacio
  64. */
  65. public class PracticaSDHIlos {
  66.  
  67. /**
  68. * @param args the command line arguments
  69. */
  70. public static void main(String[] args) {
  71. // TODO code application logic here
  72. double prueba = 100;
  73. Thread Bolt = new Thread (new Corredor(prueba),"Bolt");
  74. Thread Aranguren = new Thread (new Corredor(prueba),"Aranguren");
  75. Thread Lewis = new Thread (new Corredor(prueba), "Lewis");
  76. Thread Powell = new Thread (new Corredor(prueba), "Powell");
  77. Thread Harris = new Thread (new Corredor(prueba), "Harris");
  78. Bolt.start();
  79. Aranguren.start();
  80. Lewis.start();
  81. Powell.start();
  82. Harris.start();
  83.  
  84. }
  85.  
  86. }
Add Comment
Please, Sign In to add comment