Answer to Question #253497 in Python for shishi

Question #253497

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.


1
Expert's answer
2021-10-21T14:26:18-0400
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();
    }
    }
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS