话不多说,上代码
- 用synchronized + if 实现,这时只有AB两个线程时没有问题
package com.example.demo02;
/**
* 注释
*
* @author sunhao
* @Date 2021-08-30-21:41
*/
public class Test03 {
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();
}
}
// 判断等待, 业务, 通知
class Data{
private int num = 0;
/**
* 生产者
* @throws InterruptedException
*/
public synchronized void increment() throws InterruptedException {
if (num != 0) {
// 等待
this.wait();
}
// 业务
num ++;
System.out.println(Thread.currentThread().getName() + "=>" + num);
// 通知
this.notifyAll();
}
/**
* 消费者
* @throws InterruptedException
*/
public synchronized void decrement() throws InterruptedException {
if (num == 0) {
// 等待
this.wait();
}
// 业务
num --;
System.out.println(Thread.currentThread().getName() + "=>" + num);
// 通知
this.notifyAll();
}
}
实验结果
- 用synchronized + if 实现,但是有AC(生产者) BD(消费者)四个线程
假如 BD 的if判断了 num == 0 这时候两个线程都去wait了 这时 A 执行了 num ++ 并且notifyAll() 唤醒了 BD 然后BD一起往下走 执行了num – 就出现了负数的情况
package com.example.demo02;
/**
* 注释
*
* @author sunhao
* @Date 2021-08-30-21:41
*/
public class Test03 {
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();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "C").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "D").start();
}
}
// 判断等待, 业务, 通知
class Data{
private int num = 0;
/**
* 生产者
* @throws InterruptedException
*/
public synchronized void increment() throws InterruptedException {
if (num != 0) {
// 等待
this.wait();
}
// 业务
num ++;
System.out.println(Thread.currentThread().getName() + "=>" + num);
// 通知
this.notifyAll();
}
/**
* 消费者
* @throws InterruptedException
*/
public synchronized void decrement() throws InterruptedException {
if (num == 0) {
// 等待
this.wait();
}
// 业务
num --;
System.out.println(Thread.currentThread().getName() + "=>" + num);
// 通知
this.notifyAll();
}
}
先看一下错误示例
- 用synchronized + while 实现,有AC(生产者) BD(消费者)四个线程
假如 BD 的while判断了 num == 0 这时候两个线程都去wait了 这时 A 执行了 num ++ 并 且notifyAll() 唤醒了 BD 然后BD一起往下走 B执行了num-- 变成了0 这时D线程的while条件还是num==0 继续等待 结果正确
具体原因
因为if只会执行一次,执行完会接着向下执行if()外边的
而while不会,直到条件满足才会向下执行while()外边的
package com.example.demo02;
/**
* 注释
*
* @author sunhao
* @Date 2021-08-30-21:41
*/
public class Test03 {
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();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "C").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "D").start();
}
}
// 判断等待, 业务, 通知
class Data{
private int num = 0;
/**
* 生产者
* @throws InterruptedException
*/
public synchronized void increment() throws InterruptedException {
while (num != 0) {
// 等待
this.wait();
}
// 业务
num ++;
System.out.println(Thread.currentThread().getName() + "=>" + num);
// 通知
this.notifyAll();
}
/**
* 消费者
* @throws InterruptedException
*/
public synchronized void decrement() throws InterruptedException {
while (num == 0) {
// 等待
this.wait();
}
// 业务
num --;
System.out.println(Thread.currentThread().getName() + "=>" + num);
// 通知
this.notifyAll();
}
}
- 用Lock + while 实现,有AC(生产者) BD(消费者)四个线程
package com.example.demo02;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* 注释
*
* @author sunhao
* @Date 2021-08-30-21:41
*/
public class Test04 {
public static void main(String[] args) {
Data2 data2 = new Data2();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data2.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "a").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data2.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "b").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data2.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "c").start();
new Thread(()->{
for (int i = 0; i < 10; i++) {
try {
data2.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "d").start();
}
}
// 判断等待, 业务, 通知
class Data2{
private int num = 0;
// Lock 锁 替代 synchronized
Lock lock = new ReentrantLock();
// 一个Condition只为一个业务服务
final Condition incrementCondition = lock.newCondition();
final Condition decrementCondition = lock.newCondition();
// condition.await(); wait
// condition.signalAll(); notifyAll
/**
* 生产者
* @throws InterruptedException
*/
public void increment() throws InterruptedException {
lock.lock();
try {
while (num != 0) {
// 等待
incrementCondition.await();
}
// 业务
num ++;
System.out.println(Thread.currentThread().getName() + "=>" + num);
// 通知
decrementCondition.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
/**
* 消费者
* @throws InterruptedException
*/
public void decrement() throws InterruptedException {
lock.lock();
try {
while (num == 0) {
// 等待
decrementCondition.await();
}
// 业务
num --;
System.out.println(Thread.currentThread().getName() + "=>" + num);
// 通知
incrementCondition.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
}
还有一个最关键的问题没有解决 不是按ABCD这个顺序执行的
???????????????????????