如何避免系统因无限循环而挂起?

How to avoid system hang because of infinite loop?

提问人:Hideki Namikawa 提问时间:10/24/2023 更新时间:10/24/2023 访问量:39

问:

我将 Java 与 Sublime 一起使用,并将输出打印在一个单独的文件中,而不是在终端中。当我遇到无限循环时,太多的东西被打印到文件中,系统有点挂起,sublime 开始抛出通知以将文件重新加载到磁盘上。当我重新加载时,代码编辑很慢,直到我重新打开 Sublime。

在我完全错过它们的情况下,如何避免无限循环的这些崩溃?

顺便说一句,这是我的样板。

import java.io.*;
import java.util.*;
public class testing {
    public static class Task {
        public void solve(Scanner sc, PrintWriter pw) throws IOException {
            
    }
}
public static void main(String... args) throws IOException {
    Scanner sc = new Scanner(new FileReader(System.getenv("INPUT")));
    PrintWriter pw = new PrintWriter(new BufferedOutputStream(new FileOutputStream(System.getenv("OUTPUT"))));
    // Scanner sc = new Scanner(System.in);
    // PrintWriter pw = new PrintWriter(new BufferedOutputStream(System.out));
    Task t = new Task();
    int T = sc.nextInt();
    while (T-- > 0)
        t.solve(sc, pw);
    pw.close();
}

}

class Scanner {
    private StringTokenizer st;
    private BufferedReader br;
    public Scanner(InputStream s) {
        br = new BufferedReader(new InputStreamReader(s));
    }
    public Scanner(FileReader s) throws FileNotFoundException {
        br = new BufferedReader(s);
    }
    public String next() throws IOException {
        while (st == null || !st.hasMoreTokens()) {
            String line = br.readLine();
            if (line == null) {
                return null;
            }
            st = new StringTokenizer(line);
        }
        return st.nextToken();
    }

public int nextInt() throws IOException {
    String token = next();
    if (token == null) {
        // Handle the case where there's no more input
        // You can throw an exception, return a default value, or handle it as needed.
        return 0;
    }
    return Integer.parseInt(token);
}
public long nextLong() throws IOException {
    return Long.parseLong(next());
}
public String nextLine() throws IOException {
    return br.readLine();
}
public double nextDouble() throws IOException {
    return Double.parseDouble(next());
}
public int[] readIntArray(int n) throws IOException {
    int[] arr = new int[n];
    for (int i = 0; i < n; i++)
        arr[i] = nextInt();
    return arr;
}
public long[] readLongArray(int n) throws IOException {
    long[] arr = new long[n];
    for (int i = 0; i < n; i++)
        arr[i] = nextLong();
    return arr;
}

public boolean ready() throws IOException {
    return br.ready();
}
public void trace(int[] arr) {
    System.err.println(Arrays.toString(arr));
}
public void trace(Integer[] arr) {
    System.err.println(Arrays.toString(arr));
}
public void trace(long[] arr) {
    System.err.println(Arrays.toString(arr));
}
public void trace(Long[] arr) {
    System.err.println(Arrays.toString(arr));
}
public void trace(char[] arr) {
    System.err.println(Arrays.toString(arr));
}
public void trace(Character[] arr) {
    System.err.println(Arrays.toString(arr));
}
public void trace(Collection<?> collection) {
    System.err.println(collection);
}
public void trace(Map<?, ?> map) {
    System.err.println(map);
}
public void trace(int a, int... b) {
    System.err.print(a + " ");
    for (int i = 0; i < b.length - 1; i++) {
        System.err.print(b[i] + " ");
    }
    System.err.println(b[b.length - 1]);
}
public void trace(double a, double... b) {
    System.err.print(a + " ");
    for (int i = 0; i < b.length - 1; i++) {
        System.err.print(b[i] + " ");
    }
    System.err.println(b[b.length - 1]);
}
public void trace(long a, long... b) {
    System.err.print(a + " ");
    for (int i = 0; i < b.length - 1; i++) {
        System.err.print(b[i] + " ");
    }
    System.err.println(b[b.length - 1]);
}
public void trace(char a, char... b) {
    System.err.print(a + " ");
    for (int i = 0; i < b.length - 1; i++) {
        System.err.print(b[i] + " ");
    }
    System.err.println(b[b.length - 1]);
}
}
java 循环 错误处理 while-loop sublimetext3

评论

2赞 Gyro Gearloose 10/24/2023
从理论上讲,这是做不到的,见 en.wikipedia.org/wiki/Halting_problem。在实践中,只需在应用程序中放置一个计数器,如果计数器超出预期结果,则中止。并抛出一个异常,这样你就可以得到关于出了什么问题的提示。
0赞 OdatNurd 10/24/2023
就 Sublime 方面而言,它在重新启动时保留了窗口的状态,其中不仅包括打开的文件列表,还包括未保存的更改。如果您的输出文件是打开的并且非常大,那么 Sublime 将尝试重新打开它(或者更糟糕的是,如果您修改内容,请将整个内容存储为未保存的更改);因此,在重新启动之前,可能会从磁盘中删除输出文件。根据你的描述很难说。

答: 暂无答案