Java多线程中条件判断
一、使用if判断造成的线程虚假唤醒问题
我们在使用线程时,进行条件判断时,往往会先考虑使用if进行判断,在线程进行等待时就会出现不确定的结果。先来看看两个线程下的操作。
/**
多线程编程模式
1、高聚低合前提下,线程操作资源类
2、判断/干活/通知
3、防止虚假唤醒
**/
class A {
private int nums = 0;
public synchronized void increment() throws InterruptedException {
//等待条件
if (nums != 0) {
//等待
this.wait();
}
nums++;
System.out.println(Thread.currentThread().getName() + "->=" + nums);
//通知其它线程,我+1完成了
this.notifyAll();
}
public synchronized void decrement() throws InterruptedException {
//等待条件
if (nums == 0) {
//等待
this.wait();
}
nums--;
System.out.println(Thread.currentThread().getName() + "->=" + nums);
//通知其它线程,我-1完成了
this.notifyAll();
}
}
public class Test2 {
public static void main(String[] args) {
A a = new A();
new Thread(() -> {
try {
for (int i = 0; i < 10; i++) {
a.increment();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "A").start();
new Thread(() -> {
try {
for (int i = 0; i < 10; i++) {
a.decrement();
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}, "B").start();
}
}
对于两个线程(一个生产,一个消费)时,可以保证正确结果,原因如下图解析:
从上图分析可以看到,每次有线程进行生产或者消费时,另一个线程会进入wait状态,直到生产或者消费完成时通知才进行后续操作。所以,对于两个线程时,使用if进行条件判断可以保证正确结果。
二、为啥要使用while来进行条件判定
使用if判断对于多个线程操作时,结果就不一定了,以四个线程为例,结果如下:
从结果可以得知,nums的结果出现了大于1
原因分析:
在Java程序中,Java语言不能真正的去操纵线程执行,线程实际运行是交由c++去处理的,这是和cpu的时间片算法有关,和操作系统有关。分析见下:
//生产者代码
public synchronized void increment() throws InterruptedException {
//生产等待条件
if (nums != 0) {
//等待
this.wait();
}
nums++;
System.out.println(Thread.currentThread().getName() + "->=" + nums);
//通知其它线程,我+1完成了
this.notifyAll();
}
//消费者代码
public synchronized void decrement() throws InterruptedException {
//消费等待条件
if (nums == 0) {
//等待
this.wait();
}
nums--;
System.out.println(Thread.currentThread().getName() + "->=" + nums);
//通知其它线程,我-1完成了
this.notifyAll();
}
所以要使用while来进行条件判断