1、 虚假唤醒问题的产生
- ①. 先来看一个例子
(一个卖面的面馆,有一个做面的厨师和一个吃面的食客,需要保证,厨师做一碗面,食客吃一碗面,不能一次性多做几碗面,更不能没有面的时候吃面;按照上述操作,进行十轮做面吃面的操作)
-
②. 代码展示(两个线程不会出现虚假唤醒问题,三个或多个线程才会出现)
class Noodles{
//面的数量
private int num = 0;
//做面方法
public synchronized void makeNoodles() throws InterruptedException {
//如果面的数量不为0,则等待食客吃完面再做面
if(num != 0){
this.wait();
}
num++;
System.out.println(Thread.currentThread().getName()+"做好了一份面,当前有"+num+"份面");
//面做好后,唤醒食客来吃
this.notifyAll();
}
//吃面方法
public synchronized void eatNoodles() throws InterruptedException {
//如果面的数量为0,则等待厨师做完面再吃面
if(num == 0){
this.wait();
}
num--;
System.out.println(Thread.currentThread().getName()+"吃了一份面,当前有"+num+"份面");
//吃完则唤醒厨师来做面
this.notifyAll();
}
}
public class Test {
public static void main(String[] args) {
Noodles noodles = new Noodles();
new Thread(new Runnable(){
@Override
public void run() {
try {
for (int i = 0; i < 10 ; i++) {
noodles.makeNoodles();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"厨师A").start();
new Thread(new Runnable(){
@Override
public void run() {
try {
for (int i = 0; i < 10 ; i++) {
noodles.eatNoodles();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"食客甲").start();
}
}
- ③. 如果有两个厨师,一个食客,都进行10次循环呢?(出现线程虚假唤醒问题)
Noodles类的代码不用动,在主类中多创建两个线程即可,主类代码如下:
public class Test {
public static void main(String[] args) {
Noodles noodles = new Noodles();
new Thread(new Runnable(){
@Override
public void run() {
try {
for (int i = 0; i < 10 ; i++) {
noodles.makeNoodles();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"厨师A").start();
new Thread(new Runnable(){
@Override
public void run() {
try {
for (int i = 0; i < 10 ; i++) {
noodles.makeNoodles();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"厨师B").start();
new Thread(new Runnable(){
@Override
public void run() {
try {
for (int i = 0; i < 10 ; i++) {
noodles.eatNoodles();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"食客甲").start();
}
}
2、解决方法
if(num != 0){
this.wait();
}
改为
while(num != 0){
this.wait();
}
if(num == 0){
this.wait();
}
改为
while(num == 0){
this.wait();
}