java.io.BufferedWriter.min(int a, int b) 有什么意义?
What's the point of java.io.BufferedWriter.min(int a, int b)?
在java.io.BufferedWriter
中有一个方法:
/**
* Our own little min method, to avoid loading java.lang.Math if we've run
* out of file descriptors and we're trying to print a stack trace.
*/
private int min(int a, int b) {
if (a < b) return a;
return b;
}
这个方法有什么意义?加载需要额外的文件描述符 java.lang.Math
?
如果我用 -verbose:class
编译 运行 空程序
public class Main {
public static void main(String[] args) {
}
}
我会进入日志:
[Loaded java.lang.Math from /opt/jdk/jdk1.8.0_65/jre/lib/rt.jar]
错误是这样说的:
如果您在尝试打印堆栈跟踪时 运行 没有文件描述符,并且您调用了 Math.min(a, b)
,并且 Math 尚未被 class 加载程序加载(不太可能但可能发生,例如在启动时),那么您甚至无法生成堆栈跟踪。所以他们将 min
直接添加到 BufferedWriter
class 以避免这种情况。
如果您的 JVM 很早就失败了,Math 可能还没有加载。在调用 main
之前,您的 JVM 会执行大量工作并运行大量代码。这意味着在你到达这一点之前可能会出错。
例如这个简单的程序
public class HowManyStrings {
public static void main(String[] args) throws IOException {
System.out.println("Hello world");
System.in.read();
}
}
创建大约 10,000 个对象。
http://vanillajava.blogspot.co.uk/2015/10/common-misconception-how-many-objects.html
在java.io.BufferedWriter
中有一个方法:
/**
* Our own little min method, to avoid loading java.lang.Math if we've run
* out of file descriptors and we're trying to print a stack trace.
*/
private int min(int a, int b) {
if (a < b) return a;
return b;
}
这个方法有什么意义?加载需要额外的文件描述符 java.lang.Math
?
如果我用 -verbose:class
public class Main {
public static void main(String[] args) {
}
}
我会进入日志:
[Loaded java.lang.Math from /opt/jdk/jdk1.8.0_65/jre/lib/rt.jar]
错误是这样说的:
如果您在尝试打印堆栈跟踪时 运行 没有文件描述符,并且您调用了 Math.min(a, b)
,并且 Math 尚未被 class 加载程序加载(不太可能但可能发生,例如在启动时),那么您甚至无法生成堆栈跟踪。所以他们将 min
直接添加到 BufferedWriter
class 以避免这种情况。
如果您的 JVM 很早就失败了,Math 可能还没有加载。在调用 main
之前,您的 JVM 会执行大量工作并运行大量代码。这意味着在你到达这一点之前可能会出错。
例如这个简单的程序
public class HowManyStrings {
public static void main(String[] args) throws IOException {
System.out.println("Hello world");
System.in.read();
}
}
创建大约 10,000 个对象。
http://vanillajava.blogspot.co.uk/2015/10/common-misconception-how-many-objects.html