The Airplane always needs to check with the Airport to see if it has an
available runway before it's able to take off or land. Simulate the above-
mentioned scenario using multi-threading.
import java.util.Random;
import java.util.concurrent.atomic.AtomicInteger;
public class Main {
private static AtomicInteger fnumber = new AtomicInteger(0);
private static volatile boolean run_a = true;
public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
new Thread(() -> {
int num = fnumber.getAndIncrement();
while (true) {
if (run_a) {
run_a = false;
try {
Thread.sleep(new Random().nextInt(3500) + 1500);
}
catch (InterruptedException e) {
}
System.out.println(num + (new Random().nextBoolean() ? " the plane has landed" : " the plane took off"));
run_a = true;
break;
}
}
}).start();
}
}
}
Comments
Leave a comment