62,635
社区成员




public final synchronized void join(long millis)
throws InterruptedException {
long base = System.currentTimeMillis();
long now = 0;
if (millis < 0) {
throw new IllegalArgumentException("timeout value is negative");
}
if (millis == 0) {
while (isAlive()) {
wait(0); //wait不应该是this.wait()吗,如果主线程中有t.join(),那么这里不应该是t.wait()吗?
}
} else {
while (isAlive()) {
long delay = millis - now;
if (delay <= 0) {
break;
}
wait(delay);
now = System.currentTimeMillis() - base;
}
}
}
public class JoinDemo {
private static int a = 0;
public static void main(String[] args) throws InterruptedException {
Thread t = new Thread(new Runnable() {
@Override
public void run() {
for (int k = 0; k < 5; k++) {
a = a + 1;
}
}
});
t.start();
t.join();
System.out.println(a);
}
}