线程通信
什么是线程通信
- 当多个线程共同操作共享的资源时,线程间通过某种方式互相告知自己的的状态,以相互协调,并避免无效的资源争夺。
线程通信的常见模型(生产者与消费者模型)
- 生产者线程负责生产数据
- 消费者线程负责消费生产者生产的数据
- 注意:生产者生产完数据应该等待自己,通知消费者消费;消费者消费完数据也应该等待自己,再通知生产者生产
Object类的等待和唤醒方法
方法名称 | 说明 |
---|---|
void wait() | 让当前线程等待并释放所占锁,直到另一个线程调用notify()方法或者notifyAll()方法 |
void notify() | 唤醒正在等待的单个线程 |
void notifyAll() | 唤醒正在等待的所有线程 |
注意
- 上述方法应该使用当前同步锁对象进行调用
案例
需求:3个生产者线程,负责生产包子,每个线程每次只能生产一个包子放在桌子上,2个消费者线程负责吃包子,没人每次只能从桌子上拿一个包子吃。
ThreadTest类
public class ThreadTest {public static void main(String[] args) {Desk desk = new Desk();// 创建3个生产者线程(3个厨师)new Thread(() -> {while (true) {desk.put();}},"厨师1").start();new Thread(() -> {while (true) {desk.put();}},"厨师2").start();new Thread(() -> {while (true) {desk.put();}},"厨师3").start();// 创建2个消费者线程(2个吃货)new Thread(() -> {while (true) {desk.get();}},"吃货1").start();new Thread(() -> {while (true) {desk.get();}},"吃货2").start();}
}
Desk类
import java.util.ArrayList;
import java.util.List;public class Desk {public List<String> list = new ArrayList<>();// 放一个包子的方法// 厨师1 厨师2 厨师3public synchronized void put(){try {String name = Thread.currentThread().getName();// 判断是否有包子if (list.size() == 0){list.add(name + "做的肉包子");System.out.println(name + "做了一个肉包子");Thread.sleep(2000);// 唤醒别人,等待自己this.notifyAll();this.wait();}else {// 有包子,不做了// 唤醒别人,等待自己this.notifyAll();this.wait();}} catch (Exception e) {e.printStackTrace();}}// 吃货1 吃货2public synchronized void get() {try {String name = Thread.currentThread().getName();// 判断是否有包子if (list.size() == 1){// 有包子,吃了System.out.println(name + "吃了:" + list.get(0));list.clear();Thread.sleep(1000);this.notifyAll();this.wait();}else {// 没有包子this.notifyAll();this.wait();}} catch (InterruptedException e) {e.printStackTrace();}}
}