Kai*_*ear 5 java eclipse multithreading busy-waiting
在大学的课程中,我们学习Threads并使用了“忙等待”方法作为Car在TrafficLight. 对于这个任务,我们构建了三个类:
TrafficLight (implements Runnable)Car (implements Runnable)Main在我们的Main班级中,我们开始了两个Threads,一个是Car,一个是TrafficLight。在Car具有布尔属性hasToWait。run()此类中的方法的工作方式是,while只要hasToWait == true. 为了改变这一点,我们notifyCar()在Car类中有一个方法,它被TrafficLight. 中的run()方法TrafficLight通过aThread.sleep()来模拟一定的等待时间。
在我的教授那里一切正常,但最终我遇到了严重的问题。只要类中的while循环Car为空。当我把一个System.out.println()-这是不是空的,它的工作原理。但如果Syso 为空,则结果是不显示方法的Text Run。当Thread.sleep()inTrafficLight为0. 比它适用于空while循环。
这是我的代码:
package trafficlight;
public class Car implements Runnable {
private boolean hasToWait = true;
public void run() {
this.crossTrafficLight();
}
public void crossTrafficLight() {
while(hasToWait){ for(int i = 0; i<20; i++){System.out.println("123");}} // Busy waiting
System.out.println("Auto fährt über Ampel");
}
public void notifyCar() {
this.hasToWait = false;
System.out.println("Test");
}
}
Run Code Online (Sandbox Code Playgroud)
package trafficlight;
public class TrafficLight implements Runnable {
private Car car;
public TrafficLight(Car car) {
this.car = car;
}
@Override
public void run() {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
this.car.notifyCar();
}
}
Run Code Online (Sandbox Code Playgroud)
package trafficlight;
public class Main {
public static void main(String[] args){
Car car = new Car();
TrafficLight tl = new TrafficLight(car);
new Thread(car).start();
new Thread(tl).start();
}
}
Run Code Online (Sandbox Code Playgroud)
问题出在哪儿?为什么它适用于我的教授,但不适用于我的计算机?我在 Eclipse Juno 中得到了 1:1 的代码,使用JRE 1.7