前言
-
1.使用wait()方法和notify()方法实现"生产者和消费者模式"
-
2.什么是"生产者模式和消费者模式"?
生产线程负责生产,消费线程负责消费
生产线程和消费线程要达到均衡
这是一种特殊的业务需求,在这种特殊的情况下需要使用wait()和notify()方法 -
3.wait方法和notify方法建立在线程同步的基础之上。因为多线程要同时操作一个仓库。有线程安全问题
-
4.wait方法的作用:o.wait()让正在o对象上活动的线程t进入等待状态,并且释放t线程之前占有的o对象的锁
-
5.notify()方法作用:o.notify()让正在o对象上等待的线程唤醒,只是通知,不会释放o对象上之前占有的锁
-
6.模拟这样一个需求:
仓库我们采用List集合
List集合中假设只能存储1个元素
1个元素就表示仓库满了
如果List集合中元素个数是0,就表示仓库空了
保证List集合中永远都是最多存储一个元素
public class ThreadTest16 {
public static void main(String[] args) {
// 首先创建一个共享的仓库
List list = new ArrayList();
// 创建两个线程对象
// 生产者线程
Thread t1 = new Thread(new Producer(list));
// 消费者线程
Thread t2 = new Thread(new Consumer(list));
t1.setName("生产者线程");
t2.setName("消费者线程");
t1.start();
t2.start();
}
}
//生产线程
class Producer implements Runnable {
private List list;
public Producer(List list) {
this.list = list;
}
@Override
public void run() {
// 一直生产(使用死循环来模拟一直生产)
while (true) {
// 给仓库对象list加锁
synchronized (list) {
if (list.size() > 0) {// list集合元素个数大于零,说明仓库已经满了
// 当前线程进入等待状态,并且释放Producer之前占有的list集合的锁
try {
list.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 程序能够执行到这里说明仓库是没有元素的,进行生产
Object obj = new Object();
list.add(obj);
System.out.println(Thread.currentThread().getName() + "----->" + obj);
// 唤醒消费者
list.notify();
}
}
}
}
//消费线程
class Consumer implements Runnable {
private List list;
public Consumer(List list) {
this.list = list;
}
@Override
public void run() {
while (true) {
synchronized (list) {
if (list.size() == 0) {// list集合元素的等于零,说明仓库是空的
try {
list.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// 程序能够执行到这里说明仓库是有元素的,进行消费
Object obj = list.remove(0);
System.out.println(Thread.currentThread().getName() + "----->" + obj);
// 唤醒生产者
list.notify();
}
}
}
}