生产者消费者问题
Synchronized 版本 wait notify
/**
* 线程之间的通信问题:走产者和消费者问题.等待唤醒,通知唤醒!
* 线程交替执行! A B两个线程操作同一个变量;当A num+1时,必须告知B num-1
*/
public class ProviderAndConsumer {
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.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
},"B").start();
}
}
//等待、业务、通知;这里如果只有一个消费者、一个生产者,使用if没问题;但是如果是多个,必须使用while
class Data {//数字 资源类
private int number = 0;
//+1
public synchronized void increment() throws InterruptedException {
while (number != 0){
//等待
this.wait();
}
number++;
System.out.println(Thread.currentThread().getName()+"=>"+number);
//通知其他线程,我+1完毕了
this.notifyAll();
}
//-1
public synchronized void decrement() throws InterruptedException {
while (number == 0){
//等待
this.wait();
}
number--;
System.out.println(Thread.currentThread().getName()+"=>"+number);
//通知其他线程,我-1完毕了
this.notifyAll();
}
}
问题存在:这里只有两个线程是安全的,但是如果有四个呢、其中两个为生产者,两个为消费者?
此时可能会导致虚假唤醒。
那么如何防止虚假唤醒呢(即为什么使用While而不使用if)?
答案:就是用if判断的话,唤醒后线程会从wait之后的代码开始运行,但是不会重新判断if条件,直接继续运行if代码块之后的代码,而如果使用while的话,也会从wait之后的代码运行,但是唤醒后会重新判断循环条件,如果不成立再执行while代码块之后的代码块,成立的话继续wait。
JUC 版本 Lock
对比: Lock替换synchronized方法和语句的使用, Condition取代了对象监视器方法的使用。
通过参考文档,我们可以通过Lock找到Condition,里面找到下面的示例代码
代码实现:
class Data2 {//数字 资源类
private int number = 0;
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
//+1
public void increment() throws InterruptedException {
lock.lock();
try {
while (number != 0){
//等待
condition.await();
}
number++;
System.out.println(Thread.currentThread().getName()+"=>"+number);
//通知其他线程,我+1完毕了
condition.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void decrement() throws InterruptedException {
lock.lock();
try {
while (number == 0){
//等待
condition.await();
}
number--;
System.out.println(Thread.currentThread().getName()+"=>"+number);
//通知其他线程,我-1完毕了
condition.signalAll();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
虽然上面的四个线程不会发生过死锁,但是线程执行的次序是乱的。
即我们无法指定某一个线程在某一个之前或之后执行!
Condition的优势
1.精准通知和唤醒;即我们可以按指定的顺序唤醒线程!
/**
* 要求:A执行完调用B;B执行完调用C;C执行完调用A。
*/
public class C {
public static void main(String[] args) {
Data data = new Data();
new Thread(()->{
for (int i = 0; i < 5;i++){
data.printA();
}
},"A").start();
new Thread(()->{
for (int i = 0; i < 5;i++){
data.printB();
}
},"B").start();
new Thread(()->{
for (int i = 0; i < 5;i++){
data.printC();
}
},"C").start();
}
}
class Data {
private Lock lock = new ReentrantLock();
private int num = 1;//1 A执行;2 B执行;3 C执行
Condition condition = lock.newCondition();
public void printA(){
lock.lock();
try {
//业务,判断——>执行->通知
while (num != 1){
//等待
condition.await();
}
System.out.println(Thread.currentThread().getName()+"AAAAA");
//唤醒指定的 ,比如B
num = 2;
condition.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void printB(){
lock.lock();
try {
while (num!=2){
condition.await();
}
System.out.println(Thread.currentThread().getName()+"BBBBB");
num = 3;
condition.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public void printC(){
lock.lock();
try {
while (num !=3){
condition.await();
}
System.out.println(Thread.currentThread().getName()+"CCCCC");
num = 1;
condition.signal();
} catch (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
通过标志位num就可以实现了!