Java虚假唤醒及如何避免虚假唤醒
先给出一段虚假唤醒的代码如下:
public class SynchronizeDemo2 {
public static void main(String[] args) {
Data2 data = new Data2();
new Thread(() -> {
for (int i = 0; i < 5; i++) {
try {
data.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "A").start();
new Thread(() -> {
for (int i = 0; i < 5; i++) {
try {
data.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "B").start();
new Thread(() -> {
for (int i = 0; i < 5; i++) {
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "C").start();
new Thread(() -> {
for (int i = 0; i < 5; i++) {
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "D").start();
}
}
//线程操作的资源类,判断等待业务、通知
class Data2 {
private int number = 0;
//+1
public synchronized void increment() throws InterruptedException {
if (number != 0) {//0
//等待
this.wait();
}
number++;
System.out.println(Thread.currentThread().getName() + "=>" + number);
//通知其他线程,我+1完毕了
this.notifyAll();
}
//-1
public synchronized void decrement() throws InterruptedException {
if (number == 0) {
this.wait();
}
number--;
System.out.println(Thread.currentThread().getName() + "=>" + number);
//通知其他线程,我-1完毕了
this.notifyAll();
}
}
//执行以上的代码结果输出如下:
A=>1
C=>0
B=>1
A=>2
B=>3
C=>2
C=>1
C=>0
B=>1
A=>2
B=>3
C=>2
B=>3
A=>4
D=>3
D=>2
D=>1
D=>0
A=>1
D=>0
public class SynchronizeDemo3 {
public static void main(String[] args) {
Data3 data = new Data3();
new Thread(() -> {
for (int i = 0; i < 5; i++) {
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "A").start();
new Thread(() -> {
for (int i = 0; i < 5; i++) {
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "B").start();
new Thread(() -> {
for (int i = 0; i < 5; i++) {
try {
data.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "C").start();
new Thread(() -> {
for (int i = 0; i < 5; i++) {
try {
data.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "D").start();
}
}
//线程操作的资源类,判断等待业务、通知
class Data3 {
private int number = 0;
//+1
public synchronized void increment() throws InterruptedException {
while (number != 0) {//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();
}
}
//线程操作的资源类,判断等待业务、通知
class Data3 {
private int number = 0;
//+1
public synchronized void increment() throws InterruptedException {
while (number != 0) {//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();
}
}
//多次运行代码输出如下:
C=>1
B=>0
C=>1
A=>0
D=>1
B=>0
C=>1
A=>0
D=>1
B=>0
C=>1
A=>0
D=>1
B=>0
C=>1
B=>0
D=>1
A=>0
D=>1
A=>0