JUC(六) - Lock 同步锁(2)
2020-05-18 本文已影响0人
21号新秀_邓肯
6.2 案例二
1.生产者与消费者互动
1. 方式1
1.店员
class Clerk {
private int product = 0;
/**
* 进货
*/
public synchronized void get() {
if (product >= 10) {
System.out.println("产品已满");
} else {
System.out.println(Thread.currentThread().getName()+" : " + ++product);
}
}
/**
* 卖货
*/
public synchronized void sale() {
if (product <= 0) {
System.out.println("缺货");
} else {
System.out.println(Thread.currentThread().getName()+" : " + --product);
}
}
}
2.生产者
class Productor implements Runnable {
private Clerk clerk;
public Productor(Clerk clerk) {
this.clerk = clerk;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
clerk.get();
}
}
}
3.消费者
class Consumer implements Runnable {
private Clerk clerk;
public Consumer(Clerk clerk) {
this.clerk = clerk;
}
@Override
public void run() {
for (int i = 0; i < 20; i++) {
clerk.sale();
}
}
}
结果
以上问题:重复补货,重复消费,缺乏唤醒机制
2. 方式2
image.png image.png增加唤醒机制
虽然解决了唤醒问题,但是有一直等待的风险
3. 方式3
当有多个生产者/消费者,会出现虚假唤醒
代码
public static void main(String[] args) {
Clerk clerk = new Clerk();
Productor productor = new Productor(clerk);
Consumer consumer = new Consumer(clerk);
new Thread(productor, "生产者A").start();
new Thread(consumer, "消费者B").start();
new Thread(productor, "生产者C").start();
new Thread(consumer, "消费者D").start();
}
结果
image.png解决方法:
image.png /**
* 进货
*/
public synchronized void get() {
while (product >= 1) {
System.out.println("产品已满");
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + " : " + ++product);
this.notifyAll();
}
/**
* 卖货
*/
public synchronized void sale() {
while (product <= 0) {
System.out.println("缺货");
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + " : " + --product);
this.notifyAll();
}
if 变为 while