提问人:Hideki Namikawa 提问时间:10/24/2023 更新时间:10/24/2023 访问量:39
如何避免系统因无限循环而挂起?
How to avoid system hang because of infinite loop?
问:
我将 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]);
}
}
答: 暂无答案
评论