A Corrida de Cavalos
Por: EnZ. • 23/9/2019 • Abstract • 366 Palavras (2 Páginas) • 268 Visualizações
import java.util.*;
class Cavalo implements Runnable {
public Cavalo (String nome_) { nome = nome_; }
static Random r = new Random();
public void run () {
long t = System.currentTimeMillis();
try { Thread.sleep (r.nextInt (5000)); } catch (InterruptedException ex) {}
t = System.currentTimeMillis() - t;
System.out.println (nome + " chegou em " + t + " ms");
}
private String nome;
}
class CorridaCavalos {
public static void main(String[] args) throws InterruptedException {
Thread[] cavalos = new Thread [9];
cavalos[0] = new Thread (new Cavalo ("Lua Crescente"));
cavalos[1] = new Thread (new Cavalo ("Titum"));
cavalos[2] = new Thread (new Cavalo ("Duque de Calpheon"));
cavalos[3] = new Thread (new Cavalo ("Salaminho"));
cavalos[4] = new Thread (new Cavalo ("Azarao"));
cavalos[5] = new Thread (new Cavalo ("Dandelion"));
cavalos[6] = new Thread (new Cavalo ("Doom"));
cavalos[7] = new Thread (new Cavalo ("Narc"));
cavalos[8] = new Thread (new Cavalo ("Azula"));
System.out.println ("Corrida iniciada!");
for (int i = 0; i < cavalos.length; ++i) {
cavalos [i].start();
}
for (int i = 0; i < cavalos.length; ++i) {
cavalos [i].join();
}
System.out.println ("Corrida terminada.");
}
}
...