Synchronized版生产者消费者问题(旧版)
/*
* 线程之间的通信问题:生产者消费者问题 等待唤醒 通知唤醒
* 线程交替执行 A B C D操作同一个变量 num=0
* A num+1
* B num-1
* C num+1
* D num-1
*/
public class A {
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) {
throw new RuntimeException(e);
}
}
},"A").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.decrement();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
},"B").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.increment();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
},"C").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.decrement();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
},"D").start();
}
}
//1判断等待,2业务,3通知
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.notify();
}
//-1
public synchronized void decrement() throws InterruptedException {
while(number==0){
//等待
this.wait();
}
number--;
System.out.println(Thread.currentThread().getName()+"=>"+number);
//通知其他线程,我-1完了
this.notify();
}
}
如上执行后部分结果,可以看出不会出现不安全的问题: A=>1 B=>0 A=>1 B=>0 A=>1 B=>0 A=>1 B=>0 A=>1 B=>0
如果将里面的while()循环换成if就会出现安全问题(虚假唤醒),执行后如下,会出现有2的情况: A=>1 B=>0 A=>1 B=>0 A=>1 C=>2 D=>1 D=>0
那为什么会出现这样的情况:就是用if判断的话,唤醒后线程会从wait之后的代码开始运行,但是不会重新判断if条件,直接继续运行if代码块之后的代码,而如果使用while的话,也会从wait之后的代码运行,但是唤醒后会重新判断循环条件,如果不成立再执行while代码块之后的代码块,成立的话继续wait。
这也就是为什么用while而不用if的原因了,因为线程被唤醒后,执行开始的地方是wait之后。
Lock版生产者消费者问题(新版)
public class B {
public static void main(String[] args) {
Data2 data = new Data2();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.increment();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
},"A").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.decrement();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
},"B").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.increment();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
},"C").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.decrement();
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
},"D").start();
}
}
//1判断等待,2业务,3通知
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 (Exception e) {
e.printStackTrace();
} finally {
lock.unlock();//用完锁,要释放
}
}
//-1
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();
}
}
}
如上执行后部分结果,可以看出执行的结果是无序的:
A=>1
B=>0
c=>1
B=>0
A=>1
B=>0
A=>1
B=>0
A=>1
B=>0
A=>1
D=>0
c=>1
Lock版生产者消费者问题(新版改进)
那么接下来让他变成有序的(condition同步监视器),让线程被有序唤醒,而不是乱执行:
public class C {
public static void main(String[] args) {
Data3 data=new Data3();
new Thread(()->{
for (int i = 0; i < 10; i++) {
data.printA();
}
},"A").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
data.printB();
}
},"B").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
data.printC();
}
},"C").start();
}
}
class Data3{
private Lock lock=new ReentrantLock();
//监视器
private Condition condition1=lock.newCondition();
private Condition condition2=lock.newCondition();
private Condition condition3=lock.newCondition();
private int number=1;// 1A,2B,3C
public void printA(){
lock.lock();
try {
//业务,判断->执行->通知
while (number!=1){
condition1.await();
}
System.out.println(Thread.currentThread().getName()+"=>AAAAAA");
//唤醒指定的人,B
number=2;
condition2.signal();//唤醒指定线程 B
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
lock.unlock();
}
}
public void printB(){
lock.lock();
try {
//业务,判断->执行->通知
while (number!=2){
condition2.await();
}
System.out.println(Thread.currentThread().getName()+"=>BBBBB");
//唤醒指定的线程C
number=3;
condition3.signal();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
lock.unlock();
}
}
public void printC(){
lock.lock();
try {
//业务,判断->执行->通知
while (number!=3){
condition3.await();
}
System.out.println(Thread.currentThread().getName()+"=>AAAAAA");
//唤醒指定线程C
number=1;
condition1.signal();
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
lock.unlock();
}
}
}
执行部分结果如下,可以看出是有序执行,A->B->C:
A=>AAAAAA
B=>BBBBB
C=>AAAAAA
A=>AAAAAA
B=>BBBBB
C=>AAAAAA
A=>AAAAAA
B=>BBBBB
C=>AAAAAA