一、问题描述
写一个固定容量的同步容器,拥有put、set两个方法,能够支持2个生产者线程和10个消费者线程的阻塞调用。
二、实现方案之synchronized
import java.util.LinkedList;
import java.util.List;
/**
* @author IT00ZYQ
* @date 2021/5/23 23:16
**/
public class Q02_Synchronized<T> {
private final int MAX_SIZE;
private int count = 0;
private List<T> container = new LinkedList<>();
public Q02_Synchronized(int maxSize) {
MAX_SIZE = maxSize;
}
public Q02_Synchronized() {
MAX_SIZE = 10;
}
public synchronized int getCount() {
return count;
}
public synchronized void put(T elem) {
// 要用while而不用if
// 当wait被notify时,需要重新进行判断
// 因为可能有其他生产者线程已经生产了
while (count == MAX_SIZE) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
container.add(elem);
count ++;
System.out.println(Thread.currentThread().getName() + "进行了生产,当前剩余:" + count);
// 通知消费者线程去消费
// 这里也可能叫醒另一个生产者线程,但是有while判断,不会出现问题
this.notifyAll();
}
public synchronized T get() {
while (count == 0) {
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
T elem = container.remove(0);
count --;
System.out.println(Thread.currentThread().getName() + "进行了消费,当前剩余:" + count);
// 叫醒生产者线程进行生产
this.notifyAll();
return elem;
}
}
三、实现方案之ReentrantLock
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
/**
* @author IT00ZYQ
* @date 2021/5/24 10:36
**/
public class Q02_ReentrantLock_Condition<T> {
private final Lock lock = new ReentrantLock();
private final Condition provide = lock.newCondition();
private final Condition consumer = lock.newCondition();
private final int MAX_SIZE;
private int count;
private List<T> container = new LinkedList<>();
public Q02_ReentrantLock_Condition(int maxSize) {
MAX_SIZE = maxSize;
}
public Q02_ReentrantLock_Condition() {
MAX_SIZE = 10;
}
public void put(T elem) {
lock.lock();
try {
while (count == MAX_SIZE) {
provide.await();
}
container.add(elem);
count ++;
System.out.println(Thread.currentThread().getName() + "进行了生产,当前剩余:" + count);
// 通知消费者线程进行消费
consumer.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public T get() {
T t = null;
lock.lock();
try {
while (count == 0) {
consumer.await();
}
t = container.remove(0);
count --;
System.out.println(Thread.currentThread().getName() + "进行了消费,当前剩余:" + count);
// 通知生产者线程生产
provide.signalAll();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
return t;
}
public int getCount() {
lock.lock();
try {
return count;
} finally {
lock.unlock();
}
}
}
四、测试主程序
import java.util.concurrent.CountDownLatch;
/**
* @author IT00ZYQ
* @date 2021/5/24 10:46
**/
public class Q02_MainMethod {
public static void main(String[] args) {
// 倒数门闩,用于确保2个生产者线程与10个消费者线程都运行完毕再输出容器中剩余元素个数
CountDownLatch latch = new CountDownLatch(12);
//Q02_Synchronized<String> container = new Q02_Synchronized<>();
Q02_ReentrantLock_Condition<String> container = new Q02_ReentrantLock_Condition<>();
// 两个生产者线程
new Thread(() -> {
for (int i = 0; i < 20; i++) {
container.put("1 -> 元素"+i);
}
latch.countDown();
}, "生产者线程1").start();
new Thread(() -> {
for (int i = 0; i < 20; i++) {
container.put("2 -> 元素"+i);
}
latch.countDown();
}, "生产者线程2").start();
// 10个消费者线程
for (int i = 1; i <= 10; i++) {
new Thread(() -> {
for (int j = 0; j < 3; j++) {
container.get();
}
latch.countDown();
}, "消费者" + i).start();
}
try {
// 阻塞,直至2个生产者线程与10个消费者线程都运行完毕
latch.await();
} catch (InterruptedException e) {
e.printStackTrace();
}
// 输出容器中最终剩余个数
System.out.println("最终剩余元素:" + container.getCount());
}
}

被折叠的 条评论
为什么被折叠?



