繁体   English   中英

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(不太可能,但是可能会发生,例如在启动时),那么您甚至无法生成堆栈跟踪。 因此,他们将min直接添加到BufferedWriter类中来避免这种情况。

如果您的JVM很早就失败了,则可能尚未加载Math。 您的JVM做了很多工作,并在调用main之前运行了很多代码。 这意味着在到达这一点之前,很多事情都会出错。

例如,这个简单的程序

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM