iteye_6382
iteye_6382
2012-07-27 11:16
浏览 309
已采纳

关于System.in的分析

最近在看《JAVA编程思想》,其中System.in迷惑了我。
首先看下System的源码(jdk1.6)
[code="java"]
public final static InputStream in = nullInputStream();

private static InputStream nullInputStream() throws NullPointerException {
if (currentTimeMillis() > 0) {
return null;
}
throw new NullPointerException();
}
[/code]

疑问1: 通过源码可以看到 System.in是System类的一个静态属性,其为InputStream,初始时通过nullInputStream()方法返回,但是return null?

疑问2: 写了一个测试类,通过语句System.out.println(System.in); 打印出结果为:java.io.BufferedInputStream@1fb8ee3,说明System.in是BufferedInputStream类型,如何实现的?

疑问3: 如果线程执行到System.in.read();时,未在console输入内容,线程将挂起,直到console有输入内容。如何实现的?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • weixin_42608382
    weixin_42608382 2012-07-27 13:17
    已采纳

    [code="java"]
    /**

    • The following two methods exist because in, out, and err must be
    • initialized to null. The compiler, however, cannot be permitted to
      • inline access to them, since they are later set to more sensible values
    • by initializeSystemClass(). */ private static InputStream nullInputStream() throws NullPointerException { if (currentTimeMillis() > 0) return null; throw new NullPointerException(); } [/code]

    注意上面的注释:initializeSystemClass().
    [code="java"]
    private static void initializeSystemClass() {
    props = new Properties();
    initProperties(props);
    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(new PrintStream(new BufferedOutputStream(fdOut, 128), true));
    setErr0(new PrintStream(new BufferedOutputStream(fdErr, 128), true));
    [/code]
    这里的in 不就是个buffer类型的了。咋都不看方法的注释呢!~~

    点赞 评论
  • pengyayun
    pengyayun 2012-07-27 12:30

    疑问1: currentTimeMillis()这个方法是System类的一个静态不可变方法他产生一个当前的毫秒数 所以上面的if判断成立。这个毫秒其实就是自1970年1月1日0时起的毫秒数

    点赞 评论
  • weixin_42608382
    weixin_42608382 2012-07-27 13:35

    忘了第三个问题了:
    [code="java"]
    /**

    • Instances of the file descriptor class serve as an opaque handle
    • to the underlying machine-specific structure representing an open
    • file, an open socket, or another source or sink of bytes. The
    • main practical use for a file descriptor is to create a
    • FileInputStream or FileOutputStream to
    • contain it. */ public final class FileDescriptor [/code]

    最终用的是这个类,当然最后调的还是native方法。但这里有一句话能回答你的问题。an open socket .底层是个socket啊。。那有阻塞不就是很正常了吗?

    点赞 评论

相关推荐