1. 什么是线程通信
当多个线程共同操作共享资源时,线程间通过某种方式相互告知自己的状态,以相互协调,并避免无效的资源争夺。
线程通信的前提是要保证线程安全
2. 线程通信的常见模型(生产者与消费者模型)
- 生产者线程负责生产数据
- 消费者线程负责消费数据
生产者生产完数据,应该等待自己,并通知消费者消费;
消费者消费完数据,也应该等待自己,并通知生产者生产。
3. API
方法 | 说明 |
---|---|
void wait() | 让当前线程等待并释放所占锁,直到另一个线程调用notify()方法或者notifyAll()方法 |
void notify() | 唤醒正在等待的单个线程 |
void notifyAll() | 唤醒正在等待的所有线程 |
4. 案例:吃包子
3个生产者线程,负责生产包子,并且每次只能有一个线程负责生产1个包子放在桌子上。
2个消费者线程,负责吃包子,并且每次只能有一个线程负责吃包子。
public class Desk {
private List<String> list = new ArrayList<>();
/**
* 放一个包子
* 厨师1 厨师2 厨师3
*/
public synchronized void put() {
try {
String name = Thread.currentThread().getName();
// 判断是否有包子
if (list.size() == 0) {
list.add(name + "做的肉包子");
System.out.println(name + "做的肉包子");
Thread.sleep(1000 * 2);
}
// 等待自己,唤醒别人【注意顺序:先唤醒别人在进行等待】
this.notifyAll();
this.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 取包子
* 吃货1 吃货2
*/
public 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 * 1);
}
// 等待自己,唤醒别人【注意顺序:先唤醒别人在进行等待】
this.notifyAll();
this.wait();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public class ThreadTest6 {
public static void main(String[] args) {
// 桌子
Desk desk = new Desk();
// 创建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个消费者
new Thread(() -> {
while (true) {
desk.get();
}
}, "吃货1").start();
new Thread(() -> {
while (true) {
desk.get();
}
}, "吃货2").start();
}
}