You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让抛出异常的程序持续运行?(Java Runnable场景)

Solution to Keep the Runnable Loop Running Through Exceptions

Absolutely feasible! The problem right now is that uncaught exceptions thrown by main.getDataManager().loadPlayer(player) are terminating the thread running your Runnable, which stops your 5-second execution loop entirely. We can fix this by adding proper exception handling and ensuring critical state cleanup always happens.

Step 1: Add Granular Exception Handling

Wrap the loadPlayer() call in a try-catch block to handle failures for individual players without crashing the entire batch. Then add an outer try-finally to guarantee your loading state gets reset even if a larger error occurs.

Here's the modified code:

// First, make sure isLoading is thread-safe (add volatile if it's shared across threads)
private volatile boolean isLoading = false;

this.runnable = new Runnable() {
    @Override
    public void run() {
        if (!isLoading && !playerList.isEmpty()) {
            isLoading = true;
            ArrayList<Player> clonedList = null;
            
            try {
                // Clone the list to avoid concurrent modification issues
                clonedList = (ArrayList<Player>) playerList.clone();
                playerList.clear();
                
                // Process each player with individual exception handling
                for (Player player : clonedList) {
                    try {
                        main.getDataManager().loadPlayer(player);
                    } catch (Exception e) {
                        // Log the failure (replace with your logging framework if needed)
                        System.err.printf("Failed to load player %s: %s%n", player, e.getMessage());
                        e.printStackTrace();
                    }
                }
            } catch (Exception e) {
                // Catch unexpected errors during list cloning/clearing
                System.err.println("Error preparing player list for loading: " + e.getMessage());
                e.printStackTrace();
            } finally {
                // ALWAYS reset the loading state, no matter what happens
                isLoading = false;
            }
        }
    }
};

Key Details Explained

  • Per-Player Exception Catching: If one player's load fails, the loop continues processing the rest, and the Runnable doesn't terminate.
  • Try-Finally for State Cleanup: The finally block ensures isLoading gets set back to false even if an exception occurs during list cloning or batch processing. Without this, your loop would stop executing because future runs would skip the logic due to isLoading being stuck as true.
  • Volatile for Thread Safety: Marking isLoading as volatile ensures all threads see the latest value of the variable, preventing race conditions if multiple threads interact with this state.

Ensuring the Scheduler/Thread Keeps Running

If you're using a ScheduledExecutorService to run this Runnable every 5 seconds (the standard way to handle recurring tasks), the above changes are enough. The executor will continue scheduling future runs as long as the Runnable doesn't throw uncaught exceptions.

If you're using a manual while(true) loop with Thread.sleep(), wrap the entire loop body in a try-catch to handle interrupts and unexpected errors:

new Thread(() -> {
    while (true) {
        try {
            runnable.run();
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            // Handle thread interruption (optional: exit or continue)
            Thread.currentThread().interrupt(); // Preserve interrupt status
            break;
        } catch (Exception e) {
            // Catch any remaining uncaught exceptions to keep the loop alive
            System.err.println("Unexpected error in execution loop: " + e.getMessage());
            e.printStackTrace();
        }
    }
}).start();

Final Verdict

This approach guarantees your loop will keep running indefinitely, even when loadPlayer() throws exceptions. The exception handling isolates failures, state cleanup prevents deadlocks, and thread safety measures ensure consistent behavior across concurrent access.

内容的提问来源于stack exchange,提问作者Albert451

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 04:02:10