326等待唤醒案例代码实现和327Object类中wait带参方法和notifyAll方法
等待唤醒案例:线程之间的通信
创建一个顾客线程(消费者):告知老板要的包子的种类和数量,调用wait方法,放弃cpu的执行,进入到wAITING状态(无限等待)
创建一个老板线程(生产者):花了5秒做包子,做好包子之后,调用notify方法,唤醒顾客吃包子
注意:
顾客和老板线程必须使用同步代码块包裹起来,保证等待和唤醒只能有一个在执行
同步使用的锁对象必须保证唯一
只有锁对象才能调用wait和notify方法
Obejct类中的方法
void wait(
在其他线程调用此对象的notify()方法或notifyAll()方法前,导致当前线程等待。
void notify
唤醒在此对象监视器上等待的单个线程。会继续执行wait方法之后的代码
public static void main(String[] args) {
//创建锁对象,保证唯一
Object obj = new Object();
//创建一个顾客线程(消费者)
new Thread() {
@Override
public void run() {
while (true){
//保证等待和唤醒的线程只能有一-个执行,需要使用同步技术
synchronized (obj) {
System.out.println("告知老板要的包子的种类和数量");
//调用wait方法,放弃cpu的执行,进入到WwAI TING状态(无限等待)
try {
obj.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
//唤醒之后执行的代码
System.out.println("包子已经做好了,开吃");
System.out.println("=========================");
}
}
}
}.start();
//创建一个老板线程(生产者)
new Thread() {
@Override
public void run() {
while (true){
//花了5秒做包子
try {
Thread.sleep(5000);//花5秒钟做包子
} catch (InterruptedException e) {
e.printStackTrace();
}
//保证等待和唤醒的线程只能有-一个执行,需要使用同步技术
synchronized (obj) {
System.out.println("老板5秒钟之后做好包子,告知顾客,可以吃包子了");
//做好包子之后,调用notify方法,唤醒顾客吃包子
obj.notify();
}
}
}
}.start();
}
Object类中wait带参方法和notifyAll方法
Object类中wait带参方法和notifyAll
进入到Timewaiting(计时等待)有两种方式
1.使用sLeep(long m)方法,在毫秒值结束之后,线程睡醒进入到Runnable/Blocked状态
2.使用wait(long m)方法, wait方法如果在毫秒值结束之后,还没有被notify唤醒,就会自动醒来,线程睡醒进入到Runnable/Blocked状态
唤醒的方法:
void notify() 唤醒在此对象监视器上等待的单个线程。
void notifyAll()唤醒在此对象监视器上等待的所有线程。
方法说明
1、wait()、notify/notifyAll() 方法是Object的本地final方法,无法被重写。
2、wait()执行后拥有当前锁的线程会释放该线程锁,并处于等待状态(等待重新获取锁)
3、notify/notifyAll() 执行后会唤醒处于等待状态线程获取线程锁、只是notify()只会随机唤醒其中之一获取线程锁,notifyAll() 会唤醒所有处于等待状态的线程抢夺线程锁。
三个方法都 必须 在synchronized 同步关键字所限定的作用域中调用,否则会报错java.lang.IllegalMonitorStateException ,意思是因为没有同步,所以线程对对象锁的状态是不确定的,不能调用这些方法。
- wait 表示持有对象锁的线程A准备释放对象锁权限,释放cpu资源并进入等待。
- notify 表示持有对象锁的线程A准备释放对象锁权限,通知jvm唤醒某个竞争该对象锁的线程X。线程A synchronized 代码作用域结束后,线程X直接获得对象锁权限,其他竞争线程继续等待(即使线程X同步完毕,释放对象锁,其他竞争线程仍然等待,直至有新的notify ,notifyAll被调用)。
- notifyAll 表示持有对象锁的线程A准备释放对象锁权限,通知jvm唤醒所有竞争该对象锁的线程,线程A synchronized 代码作用域结束后,jvm通过算法将对象锁权限指派给某个线程X,所有被唤醒的线程不再等待。线程X synchronized 代码作用域结束后,之前所有被唤醒的线程都有可能获得该对象锁权限,这个由JVM算法决定。
wait有三个重载方法,同时必须捕获非运行时异常InterruptedException。
- wait() 进入等待,需要notify ,notifyAll才能唤醒
- wait(long timeout) 进入等待,经过timeout 超时后,若未被唤醒,则自动唤醒
- wait(timeout, nanos) 进入等待,经过timeout 超时后,若未被唤醒,则自动唤醒。相对wait(long timeout) 更加精确时间。
public class WaitAndNotify {
public static void main(String[] args) {
MethodClass methodClass = new MethodClass();
Thread t1 = new Thread(() -> {
try {
methodClass.product();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}, "t1");
Thread t2 = new Thread(() -> {
try {
methodClass.customer();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}, "t2");
Thread t3 = new Thread(() -> {
try {
methodClass.customer();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}, "t3");
t1.start();
t2.start();
t3.start();
}
}
class MethodClass {
// 定义生产最大量
private final int MAX_COUNT = 20;
int productCount = 0;
public synchronized void product() throws InterruptedException {
while (true) {
System.out.println(Thread.currentThread().getName() + ":::run:::" + productCount);
Thread.sleep(10);
if (productCount >= MAX_COUNT) {
System.out.println("货舱已满,,.不必再生产");
wait();
} else {
productCount++;
}
notifyAll();
}
}
public synchronized void customer() throws InterruptedException {
while (true) {
System.out.println(Thread.currentThread().getName() + ":::run:::" + productCount);
Thread.sleep(10);
if (productCount <= 0) {
System.out.println("货舱已无货...无法消费");
wait();
} else {
productCount--;
}
notifyAll();
}
}
}

浙公网安备 33010602011771号