V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
leye
V2EX  ›  Java

请教大家一个关于 join 和 sleep 的问题。

  •  1
     
  •   leye · 2019-06-16 12:12:18 +08:00 · 1740 次点击
    这是一个创建于 1775 天前的主题,其中的信息可能已经有所发展或是发生改变。
    代码如下:
    class Thread01 extends Thread {
    @Override
    synchronized public void run() {
    super.run();
    try {
    System.out.println("thread in");
    Thread.sleep(5000);
    System.out.println("thread out");
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }

    public class Test05 {
    public static void main(String[] args) {
    try {
    Thread01 thread01 = new Thread01();
    thread01.start();
    System.out.println("" + System.currentTimeMillis());
    thread01.join(2000);
    System.out.println("main end");
    System.out.println("" + System.currentTimeMillis());
    } catch (InterruptedException e) {
    e.printStackTrace();
    }
    }
    }

    我有点疑惑的是在我的电脑上,为什么两次的时间间隔是近乎为 5 秒。2 秒种之后没有获得锁吗?希望您恰巧了解的话可以指定一下迷津,多谢了。
    1 条回复    2019-06-16 12:23:56 +08:00
    leye
        1
    leye  
    OP
       2019-06-16 12:23:56 +08:00
    自己加了把锁,没看到,晕
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   3264 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 14:10 · PVG 22:10 · LAX 07:10 · JFK 10:10
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.