按照下面的代码,我有两个类A的实例-a1和a2。并分别对两个实例调用foo()方法。
foo()方法中有一个同步块,它被锁定在调用对象上。因为这是一个实例级锁定,所以这两个方法应该同时开始执行,因为它们是从两个不同的实例调用的。但是,他们是按顺序被处决的。
我的理解是“this”指的是调用Runnable实例,它是不同的,甚至线程也是不同的。因此,Thread.sleep不应该阻塞另一个线程。那么,为什么run的两个调用没有并行发生呢?
main <time> Inside A.run
Thread-0 <time> Inside A.run
Thread-0 <time+4s> Exiting A.run
main <time+5s> Exiting A.run
实际输出(按顺序执行)
main <time> Inside A.run
main <time+5s> Exiting A.run
Thread-0 <time+5s> Inside A.run
Thread-0 <time+9s> Exiting A.run
import java.time.*;
import java.time.format.DateTimeFormatter;
public class Test {
public static void main(String[] args) {
/*A a1 = new A(5000); A a2 = new A(4000);
a1.foo(); a2.foo();*/
A a1 = new A(5000); A a2 = new A(4000);
Thread t = new Thread(a2);
/*a1.run(); t.start();*/
t.start(); a1.run(); // <-- putting t.start() before a1.run() solves the issue
}
}
class A implements Runnable {
public long waitTime;
public A() {}
public A(long timeInMs) {
waitTime = timeInMs;
}
public void run() {
synchronized(this) {
try {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy/MM/dd HH:mm:ss");
LocalDateTime time = LocalDateTime.now();
System.out.println(Thread.currentThread().getName() + " " + formatter.format(time) + " Inside A.run");
Thread.sleep(waitTime);
time = LocalDateTime.now();
System.out.println(Thread.currentThread().getName() + " " + formatter.format(time) + " Exiting A.run");
} catch (InterruptedException e) {}
}
}
}