生產者消費者問題,用Synchronized關鍵字解決方法如下:
兩個線程操作同一個對象,一個進行+1操作,一個進行-1操作。
本代碼僅用於練習操作,不要直接在生產環境執行。
public class JUCTest01 {
public static void main(String[] args) throws InterruptedException {
Data data = new Data();
new Thread(() -> {
for (int i = 0; i < 100; i++) {
try {
data.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "A").start();
new Thread(() -> {
for (int i = 0; i < 100; i++) {
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "B").start();
}
}
//判斷是否等待,業務,幹完活通知
class Data {
private int number = 0;
public synchronized void increment() throws InterruptedException {
if (number != 0) {
//等待
this.wait();
}
number++;
//通知其他線程,我+1完畢了
System.out.println(Thread.currentThread().getName() + "執行了+1" + ";number=" + number);
this.notifyAll();
}
public synchronized void decrement() throws InterruptedException {
if (number == 0) {
//等待
this.wait();
}
number--;
//通知其他線程,我+1完畢了
System.out.println(Thread.currentThread().getName() + "執行了-1" + ";number=" + number);
this.notifyAll();
}
}
代碼執行結果:
A執行了+1;number=1
B執行了-1;number=0
A執行了+1;number=1
B執行了-1;number=0
A執行了+1;number=1
B執行了-1;number=0
A執行了+1;number=1
B執行了-1;number=0
A執行了+1;number=1
B執行了-1;number=0
上面樣例,僅存在AB兩個對變量進行操作,如果我們將代碼修改下,增加一CD等線程一起進行操作。
則會打印出number=2,3,負數等其他異常情況。
上述代碼中我們用了if去進行判斷其實是有問題的。
jdk文檔中也註明,因爲存在虛假喚醒,wait方法最好放在while循環中。
我們將上述方法if改爲while條件判斷下。
public class JUCTest01 {
public static void main(String[] args) throws InterruptedException {
Data data = new Data();
new Thread(() -> {
for (int i = 0; i < 100; i++) {
try {
data.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "A").start();
new Thread(() -> {
for (int i = 0; i < 100; i++) {
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "B").start();
new Thread(() -> {
for (int i = 0; i < 100; i++) {
try {
data.increment();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "C").start();
new Thread(() -> {
for (int i = 0; i < 100; i++) {
try {
data.decrement();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}, "D").start();
}
}
//判斷是否等待,業務,幹完活通知
class Data {
private int number = 0;
public synchronized void increment() throws InterruptedException {
while (number != 0) {
//等待
this.wait();
}
number++;
//通知其他線程,我+1完畢了
System.out.println(Thread.currentThread().getName() + "執行了+1" + ";number=" + number);
this.notifyAll();
}
public synchronized void decrement() throws InterruptedException {
while (number == 0) {
//等待
this.wait();
}
number--;
//通知其他線程,我+1完畢了
System.out.println(Thread.currentThread().getName() + "執行了-1" + ";number=" + number);
this.notifyAll();
}
}
再次執行打印結果,則可以正常輸出01。