1、实现
生产者和消费者问题 Synchronized 版
public class Communication01 {
// 线程间通信,生产者消费者问题; 等待唤醒、通知唤醒
// 多个线程交替执行 操作同一个变量 num = 0
public static void main(String[] args) {
Data data = new Data();
// new Runnable() : @FunctionalInterface 函数式接口,jdk1.8 lambda表达式 (参数)->{ 代码 }
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"A").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"B").start();
}
}
// 共享资源类 (线程执行的方法设置)
class Data{
private int num=0;
//num+1
public synchronized void increment() throws InterruptedException {
if (num!=0){
this.wait();
}
num++;
System.out.println(Thread.currentThread().getName()+": num = "+num);
//通知其他线程,num+1完成
this.notifyAll();
}
// num-1
public synchronized void decrement() throws InterruptedException {
if (num==0){
this.wait();
}
num--;
System.out.println(Thread.currentThread().getName()+": num = "+num);
//通知其他线程,num-1完成
this.notifyAll();
}
}
结果:符合
2、但存在问题:虚假唤醒
什么是虚假唤醒 ?
个人见解:
在线程通信中,当有多个线程时,就会出现虚假唤醒。
当一定的条件触发时会唤醒很多在阻塞状态的线程,但只有部分的线程唤醒是有用的,其余线程的唤醒是多余的。
比如:生产者生产了一个包子,唤醒了所有线程,包括生产者和消费者。但是只有一个线程能够拿到锁执行。
而我们希望是消费者拿到锁执行,所以我们弄了判断条件,有一个包子时生产者拿到锁也就只能wait()等待进入阻塞状态。
当我们把 num+1 方法 增加一个线程C时
// 下面省略了 Data类,跟1上面一样
public class Communication01 {
// 线程间通信,生产者消费者问题; 等待唤醒、通知唤醒
// 多个线程交替执行 操作同一个变量 num = 0
public static void main(String[] args) {
Data data = new Data();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"A").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"C").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"B").start();
}
}
结果 : 现在看结果根本离谱,并不是1、0交替。
3、问题原因
在2的基础上 (第二步创建3个线程,两个num+1 A和C,一个num-1 B),对Data(线程执行方法的类)进行一点点修改,在this.wait();
后面添加一行输出。
class Data{
private int num=0;
//num+1
public synchronized void increment() throws InterruptedException {
if (num!=0){
this.wait(); // 被唤醒后执行wait()后面的代码
System.out.println("唤醒执行!要+1了");
}
num++;
System.out.println(Thread.currentThread().getName()+": num = "+num);
//通知其他线程,num+1完成
this.notifyAll();
}
// num-1
public synchronized void decrement() throws InterruptedException {
if (num==0){
this.wait(); // 被唤醒后执行wait()后面的代码
System.out.println("唤醒执行!要-1了");
}
num--;
System.out.println(Thread.currentThread().getName()+": num = "+num);
//通知其他线程,num-1完成
this.notifyAll();
}
}
然后像2一样执行,得下面结果:
最终原因理论说辞
A、C为生产者num+1,B为消费者num-1。如果此时是A消费者在执行,B、C在调用wait()进入阻塞队列等待,当生产者A线程调用了对象的 notifyAll()方法(唤醒所有 wait 线程),阻塞队列中的所有线程都会去竞争对象锁,A线程执行完了 synchronized 代码块,它会释放掉该对象锁给阻塞队列竞争。
无论哪一个线程竞争到了锁,都会执行wait()后的代码,如果是C生产者抢到了锁,因为if只会执行一次,执行完会接着向下执行if()外边的,而wait()在if内,所以不会执行判断。而while不会,直到条件满足才会向下执行while()外边的
4、问题解决
将Data类中的方法判断if修改为while即可( wait要始终保证在while循环当中)
public class Communication02 {
// 线程间通信,生产者消费者问题; 等待唤醒、通知唤醒
// 多个线程交替执行 操作同一个变量 num = 0
public static void main(String[] args) {
Data1 data1 = new Data1();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data1.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"A").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data1.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"B").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data1.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"C").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data1.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"D").start();
}
}
class Data1{
private int num=0;
//num+1
public synchronized void increment() throws InterruptedException {
while (num!=0){
this.wait(); // 被唤醒后执行wait()后面的代码
//System.out.println("唤醒执行!要+1了");
}
num++;
System.out.println(Thread.currentThread().getName()+": num = "+num);
//通知其他线程,num+1完成
this.notifyAll();
}
// num-1
public synchronized void decrement() throws InterruptedException {
while (num==0){
this.wait(); // 被唤醒后执行wait()后面的代码
// System.out.println("唤醒执行!要-1了");
}
num--;
System.out.println(Thread.currentThread().getName()+": num = "+num);
//通知其他线程,num-1完成
this.notifyAll();
}
}
结果:得到解决