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)在无锁竞争情况下,在等待时间过去后就直接重新获取锁,往后执行;但是在竞争条件下,都会等获取到锁了才可以往下执行
附:Java线程状态
Java的Thread类中定义了一个线程状态的枚举,State
public enum State {
/**
* 线程还未启动
*/
NEW,
/**
* Runnable的线程已经在JVM中执行了,但是可能还在等待OS的一些资源例如处理器
*/
RUNNABLE,
/**
* 线程处于等待一个monitor锁的状态
* 1.等待进入synchronized块或方法
* 2.在调用Object.wait后重新进入同步块
*/
BLOCKED,
/**
* 假设线程A进入waiting状态,事实上是等待其他的线程B去执行某个特殊的操作
* 可以让线程进入waiting状态的几种情况:
* 1.Object#wait() 等待线程B调用Object.notify()或者Object.notifyAll()
* 2.B.join() 等待线程B结束
* 3.LockSupport.park() 等待线程B调用LockSupport.unpark(A);
*/
WAITING,
/**
* 有时间约束的线程等待状态
* 1. Thread.sleep}
* 2. Object.wait(long)
* 3. Thread.join(long)
* 4. LockSupport.parkNanos
* 5. LockSupport.parkUntil(long deadline)
*/
TIMED_WAITING,
/**
* 终止状态,线程执行完毕
*/
TERMINATED;
}