wait / notify
wait 會釋放鎖,但 notify 不會釋放鎖,
public class MyContainer1<T> {
private final LinkedList<T> list = new LinkedList<>();
// 最多存 10 個元素
private final int MAX = 10;
private int count = 0;
public synchronized void put(T t) {
/**
* 為什么用 while 而不用 if?
* 用 while 的目的是當執行緒被喚醒的時候再進行一次判斷
*/
while (list.size() == MAX) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
list.add(t);
count++;
// 通知消費者執行緒消費
this.notifyAll();
}
public synchronized T get() {
T t = null;
while (list.size() == 0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
t = list.removeFirst();
count--;
// 通知生產者執行緒生產
this.notifyAll();
return t;
}
public static void main(String[] args) {
MyContainer1<String> my = new MyContainer1<>();
// 啟動消費者執行緒
for (int i = 0; i < 10; i++) {
new Thread(() -> {
for (int j = 0; j < 5; j++) {
System.out.println(my.get());
}
}, "c" + i).start();
}
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 啟動生產者執行緒
for (int i = 0; i < 2; i++) {
new Thread(() -> {
for (int j = 0; j < 25; j++) {
my.put(Thread.currentThread().getName() + " " + j);
}
}, "p" + i).start();
}
}
}
這種寫法有一種缺陷:喚醒執行緒時無法明確只喚醒生產者執行緒或消費者執行緒,
await / signal
await 會釋放鎖,但 signal 不會釋放鎖,
public class MyContainer2<T> {
private final LinkedList<T> list = new LinkedList<>();
// 最多存 10 個元素
private final int MAX = 10;
private int count = 0;
private Lock lock = new ReentrantLock();
// Condition 的本質是不同的等待佇列
private Condition p = lock.newCondition();
private Condition c = lock.newCondition();
public void put(T t) {
try {
lock.lock();
while (list.size() == MAX) {
p.await();
}
list.add(t);
count++;
// 通知消費者執行緒消費
c.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public T get() {
T t = null;
try {
lock.lock();
while (list.size() == 0) {
c.await();
}
t = list.removeFirst();
count--;
// 通知生產者執行緒生產
p.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
return t;
}
public static void main(String[] args) {
MyContainer2<String> my = new MyContainer2<>();
// 啟動消費者執行緒
for (int i = 0; i < 10; i++) {
new Thread(() -> {
for (int j = 0; j < 5; j++) {
System.out.println(my.get());
}
}, "c" + i).start();
}
try {
TimeUnit.SECONDS.sleep(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
// 啟動生產者執行緒
for (int i = 0; i < 2; i++) {
new Thread(() -> {
for (int j = 0; j < 25; j++) {
my.put(Thread.currentThread().getName() + " " + j);
}
}, "p" + i).start();
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/243560.html
標籤:java
