你好,我试图了解线程,我被要求模拟元素之间的能力,比如对象之间的竞争,但我需要使用 java 线程并比较线程与可运行。我实现了以下内容:
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();
}
}
但这是正确的方法吗?为什么使用运行,为什么使用启动?我怎么知道哪个线程先出现?