我刚刚在 java 线程中发现了奇怪的行为。这是一个代码示例:
class Job extends Thread {
private Integer number = 0;
public void run() {
for (int i = 1; i < 1000000; i++) {
number++;
}
}
public Integer getNumber() {
return number;
}
}
public class Test {
public static void main(String[] args)
throws InterruptedException {
Job thread = new Job();
thread.start();
synchronized (thread) {
thread.wait();
}
System.out.println(thread.getNumber());
}
}
出乎意料的是它会打印出 999999。似乎在start() 方法逻辑的末尾有notify()调用。有任何想法吗?