Loading...
āœ“

12-Hour Money-Back Guarantee

šŸš€ Rocket Launch Countdown Sync

šŸš€ Rocket Launch Countdown Sync

šŸš€ Rocket Launch Countdown Sync

15 Jul 20253 min read

You’re simulating a rocket launch system.

Before the rocket can launch, it must pass several system checks (e.g., fuel, navigation, engine, weather, etc.).

Each system is checked independently by a different thread.

šŸ‘‰ Only after all checks are complete, the main launch system can proceed and say:

āœ… "All systems go. Launching!"

Solution Approaches

  1. Basic Thread Join Approach

    1. Create threads - One thread for each system check (fuel, navigation, etc.)

    2. Start all threads - All checks run simultaneously

    3. Wait for completion - Main thread waits until all checks finish

    4. Launch - Only proceed if all checks were successful

public class RocketLaunchBasic {
    
    // Task for checking individual systems
    static class SystemCheck implements Runnable {
        private final String systemName;
        
        public SystemCheck(String name) {
            this.systemName = name;
        }
        
        public void run() {
            try {
                // Simulate check time (0-2 seconds)
                System.out.println("ā³ Checking " + systemName + "...");
                Thread.sleep((long)(Math.random() * 2000));
                System.out.println(systemName + " āœ…");
            } catch (InterruptedException e) {
                System.out.println(systemName + " āŒ (Interrupted)");
            }
        }
    }

    public static void main(String[] args) throws InterruptedException {
        String[] systems = {"Fuel", "Navigation", "Engine", "Weather", "Comm"};
        
        // 1. Create threads for all systems
        Thread[] threads = new Thread[systems.length];
        for (int i = 0; i < systems.length; i++) {
            threads[i] = new Thread(new SystemCheck(systems[i]));
        }
        
        // 2. Start all checks in parallel
        System.out.println("šŸš€ Starting pre-launch checks...");
        for (Thread t : threads) {
            t.start(); // Launch parallel execution
        }
        
        // 3. Wait for all to complete
        for (Thread t : threads) {
            t.join(); // Block until thread finishes
        }
        
        // 4. Final verification
        System.out.println("\nšŸ” All checks completed!");
        System.out.println("šŸš€ All systems go. Launching!");
    }
}

āŒ Failure Test Cases

1. Thread Never Completes (Deadlock)

One system check hangs indefinitely. Lets say one of started thread never finish its run.

public void run() {
    if (systemName.equals("Fuel")) {
        while(true) { /* Infinite loop */ } 
    }
    // ... normal check ...
}

ā³ Checking Fuel...
ā³ Checking Navigation...
Navigation āœ… ...
(Program hangs forever)

To fix this, use join(timeout), which will only wait for specific amount of time

for (Thread t : threads) {
    t.join(3000); // Wait max 3 seconds
    if (t.isAlive()) {
        System.out.println("ā° " + systemName + " timeout!");
        t.interrupt();
    }
}

2. Exception in System Check

Thread crashes during check.

public void run() {
    if (systemName.equals("Navigation")) {
        throw new RuntimeException("Sensor error");
    }
    // ... normal check ...
}

ā³ Checking Fuel...
ā³ Checking Navigation...
ā³ Checking Engine...
Exception in thread "Thread-1" java.lang.RuntimeException: Sensor error
Fuel āœ…
Engine āœ…

šŸ” All checks completed!
šŸš€ All systems go. Launching! (āš ļø Despite failure!)