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

2019-06-16 12:12:18 +08:00
 leye
代码如下:
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 秒种之后没有获得锁吗?希望您恰巧了解的话可以指定一下迷津,多谢了。
1779 次点击
所在节点    Java
1 条回复
leye
2019-06-16 12:23:56 +08:00
自己加了把锁,没看到,晕

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/574405

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX