代码如下:
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 秒种之后没有获得锁吗?希望您恰巧了解的话可以指定一下迷津,多谢了。