Java的Object.wait(long)在等待时间过去后会继续往后执行吗
Object.wait(long)方法相比于wait,多了个等待时长,那么当等待时长过去后,线程会继续往下执行吗?
单个线程执行
多个线程并发执行
public class ThreadWaitDemo {
public static final int WAIT_IN_SECONDS = 2;
public static final int NOTIFY_IN_SECONDS = 6;
public static void main(String[] args) throws InterruptedException {
ThreadWaitDemo threadWait = new ThreadWaitDemo();
Thread a = new Thread(threadWait::justWaitAndPrint, "t1");
Thread b = new Thread(threadWait::justNotify, "t2");
a.start();
// ① 先注释这段,让t1自己执行
b.start();
}
public synchronized void justWaitAndPrint() {
try {
System.out.println(Thread.currentThread().getName() + ": I am in");
System.out.println(Thread.currentThread().getName() + ": I am gonna wait");
long start = System.currentTimeMillis();
wait(WAIT_IN_SECONDS * 1000);
// 如果时间过了就可以解除等待状态的话,那么这里的时间间隔就会接近2s
System.out.println("wait end " + (System.currentTimeMillis() - start) / 1000.0 + "seconds");
} catch (Exception e) {
System.out.println(Thread.currentThread().getName() + ": Oops, something bad happen " + e);
}
System.out.println(Thread.currentThread().getName() + ": I am out");
}
public synchronized void justNotify() {
try {
TimeUnit.SECONDS.sleep(NOTIFY_IN_SECONDS);
System.out.println(Thread.currentThread().getName() + ": I am in");
} catch (Exception e) {
System.out.println(Thread.currentThread().getName() + ": Oops, something bad happen " + e);
}
System.out.println(Thread.currentThread().getName() + ": I am out");
}
}
第一种情况下,运行结果
t1: I am in
t1: I am gonna wait
wait end 2.004seconds
t1: I am out
第二种情况下,运行结果
t1: I am in
t1: I am gonna wait
t2: I am in
t2: I am out
wait end 6.002seconds // 等待时间会随NOTIFY_IN_SECONDS变化
t1: I am out
结论:wait(long)在无锁竞争情况下,在等待时间过去后就直接重新获取锁,往后执行;但是在竞争条件下,都会等获取到锁了才可以往下执行