「System源码java」systematic code
今天给各位分享System源码java的知识,其中也会对systematic code进行解释,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在开始吧!
本文目录一览:
- 1、java源码的问题System.out.println(str)
- 2、java System 类里out和err怎么赋值的?没找到源码 initializeSystemClass也没有找到....求大神讲解一下
- 3、java System.nanoTime(),源码到底怎么算的
java源码的问题System.out.println(str)
system类最后有这么一个方法initializeSystemClass()
这个方法有setOut0(new PrintStream(new BufferedOutputStream(fdOut, 128), true));
这句话就初始化了out,所以我们用的out并不是null
java System 类里out和err怎么赋值的?没找到源码 initializeSystemClass也没有找到....求大神讲解一下
这是 initializeSystemClass 源码,就在 System 类中,在System类的源码中搜索一下 initializeSystemClass 就可以找到了。
private static void initializeSystemClass() {
props = new Properties();
initProperties(props); // initialized by the VM
sun.misc.VM.saveAndRemoveProperties(props);
lineSeparator = props.getProperty("line.separator");
sun.misc.Version.init();
FileInputStream fdIn = new FileInputStream(FileDescriptor.in);
FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err);
setIn0(new BufferedInputStream(fdIn));
setOut0(newPrintStream(fdOut, props.getProperty("sun.stdout.encoding")));
setErr0(newPrintStream(fdErr, props.getProperty("sun.stderr.encoding")));
loadLibrary("zip");
// Setup Java signal handlers for HUP, TERM, and INT (where available).
Terminator.setup();
sun.misc.VM.initializeOSEnvironment();
Thread current = Thread.currentThread();
current.getThreadGroup().add(current);
// register shared secrets
setJavaLangAccess();
sun.misc.VM.booted();
}
其中这几句代码就是用来初始化,标准输入流,标准输出流,标准错误输出流的。
FileInputStream fdIn = new FileInputStream(FileDescriptor.in);
FileOutputStream fdOut = new FileOutputStream(FileDescriptor.out);
FileOutputStream fdErr = new FileOutputStream(FileDescriptor.err);
setIn0(new BufferedInputStream(fdIn));
setOut0(newPrintStream(fdOut, props.getProperty("sun.stdout.encoding")));
setErr0(newPrintStream(fdErr, props.getProperty("sun.stderr.encoding")));
这其中 setOut0 就是用来设置 System.out 的,setOut0 看不到源码的,它是在java虚拟中的。
java System.nanoTime(),源码到底怎么算的
nanoTime()返回最准确的可用系统计时器的当前值,以毫微秒为单位。
此方法只能用于测量已过的时间,与系统或钟表时间的其他任何时间概念无关。返回值表示从某一固定但任意的时间算起的毫微秒数(或许从以后算起,所以该值可能为负)。此方法提供毫微秒的精度,但不是必要的毫微秒的准确度。它对于值的更改频率没有作出保证。在取值范围大于约 292 年(263 毫微秒)的连续调用的不同点在于:由于数字溢出,将无法准确计算已过的时间。
例如,测试某些代码执行的时间长度:
long startTime = System.nanoTime();
// ... the code being measured ...
long estimatedTime = System.nanoTime() - startTime;
返回:
系统计时器的当前值,以毫微秒为单位。
从以下版本开始:
1.5
参考文献:API
然后我又在网上搜了一下,看到了这个,不知道对你有没有帮助。
System源码java的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于systematic code、System源码java的信息别忘了在本站进行查找喔。
发布于:2022-11-22,除非注明,否则均为
原创文章,转载请注明出处。