V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
leye
V2EX  ›  Java

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

  •  1
     
  •   leye · Jun 16, 2019 · 2583 views
    This topic created in 2519 days ago, the information mentioned may be changed or developed.
    代码如下:
    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 replies    2019-06-16 12:23:56 +08:00
    leye
        1
    leye  
    OP
       Jun 16, 2019
    自己加了把锁,没看到,晕
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2671 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 15:50 · PVG 23:50 · LAX 08:50 · JFK 11:50
    ♥ Do have faith in what you're doing.