public static void main(String[] args){
ExecutorService executorService = Executors.newCachedThreadPool();
WaitNotifyExample exam = new WaitNotifyExample();
executorService.execute(() -> exam.after());
executorService.execute(() -> exam.before());
}
}
class WaitNotifyExample{
private Lock lock = new ReentrantLock();
public synchronized void before() {
System.out.println("before func");
notifyAll();
// 唤醒所有线程
}
public synchronized void after(){
try {
wait();
// 执行wait() 操作将线程挂起,该线程会释放锁。 否则其他线程无法进入对象的同步方法或者同步控制块中,
// 就无法执行notify()或notifyAll() 就会造成 死锁
} catch (InterruptedException e){
e.printStackTrace();
}
System.out.println("after func");
}
}
输出: ![](https://img-blog.csdnimg.cn/20190406170035349.png)
#### 二、wait() 和 sleep() 的区别
#### 同:
1. 都是线程同步时会用到的方法,使当前线程暂停运行,把机会交给其他线程
2.如果任何线程在等待期间被中断都会抛出InterruptedException
3.都是native() 方法
#### 异:
1. wait() 是Object超类中的方法;而sleep()是线程Thread类中的方法
2. 对锁的持有不同,**wait()会释放锁,而sleep()并不释放锁**
3. 唤醒方法不完全相同,wait() 依靠notify或者notifyAll 、中断、达到指定时间来唤醒;而sleep()到达指定时间被唤醒.
4. 使用位置不同,**wait只能在同步代码块或同步控制块中使用,而sleep可以在任何位置使用**。
#### 三、await() signal() 和 signalAll()
java.util.concurrent类库中提供的Condition类来实现线程之间的协调。
在Condition上调用 await() 方法使线程等待,其他线程调用signal() 或 signalAll() 方法唤醒等待的线程。
ReentrantLock里面默认有实现newCondition()方法,新建一个条件对象。
下面使用Lock来获取一个Condition对象,使用**重入锁ReentrantLock**。
代码示例:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class Thread_await_signal {
public static void main(String[] args){
ExecutorService executorService = Executors.newCachedThreadPool();
AwaitSignalExample exam = new AwaitSignalExample();
executorService.execute(() -> exam.after());
executorService.execute(() -> exam.before());
}
}
// java.util.concurrent类库中提供了Condition类来实现线程之间的协调,可以在Condition上调用await()方法使线程挂起。
// 其他线程可以调用signal()或signalAll()来唤醒等待的线程.
// 使用Lock来获取一个Condition对象
class AwaitSignalExample{
private Lock lock = new ReentrantLock();
private Condition condition = lock.newCondition();
public void before() {
lock.lock();
try {
System.out.println("before func");
condition.signalAll();
// 唤醒挂起的其他线程
} finally {
lock.unlock();
}
}
public void after() {
lock.lock();
try {
condition.await();
System.out.println("after func");
} catch (InterruptedException e){
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
结果输出:![](https://img-blog.csdnimg.cn/20190406171226866.png)
#### 四、wait() 与 await()
1. wait()是Object超类中的方法,而await()是ConditionObject类里面的方法.
2. await会导致当前线程被阻塞,会**释放锁**,这点和wait是一样的
3. await中的lock不再使用synchronized把代码同步包装起来
4. await的阻塞需要另外的一个对象condition
5. notify是用来唤醒使用wait的线程;而signal是用来唤醒await线程。
6. 所在的超类不同使用场景也不同,**wait一般用于Synchronized中,而await只能用于ReentrantLock锁中**