hello i was trying to understand about threads and i was asked to simulate a competence between to elements like a race between to objects but i need to use java threads and compare thread vs runnable. I implemented the following:
public class lamborgini extends Thread {
public void run() {
int distance = 1000;
int steps = 0;
int velocity = 45;
int acelerationTime = 800;
while (steps < distance) {
System.out.println("Lamborgini running");
steps+=velocity;
Thread.sleep(acelerationTime);
}
}
}
public class ferrari implements Runnable {
@Override
public void run() {
int distance = 1000;
int steps = 0;
int velocity = 130;
int acelerationTime = 950;
while (steps < distance) {
System.out.println("Lamborgini running");
steps+=velocity;
Thread.sleep(acelerationTime);
}
}
}
public class RaceMain {
public static void main(String[] args){
lamborgini a = new lamborgini();
lamborgini.start();
ferrari b = new ferrari();
ferrari.run();
}
}
But is this the right way? why use run and why use start? and how can I know which of the threads come first?