这个是一个很常见的模式了,能在很大程度上反应大家对于多线程的理解,我今天就写一下,并且详细的说一下对于每个需要注意的关键点的了解。代码如下关于需要注意的地方都在代码中有了详细的说明。
public class T <V>{
final private LinkedList<V> linkedList = new LinkedList<>();
final private int MAX = 10;
private int count = 0 ;
public synchronized void put(V o){
while(linkedList.size()==MAX){
//这里为什么要用while判断呢?这是因为在生产者进行生产的时候有可能在
//判断容量已经满了才会进来,然后在进入wait状态的时候会释放锁如果有新
//生产者抢占到了CPU然后也进入了等待状态两个生产者都进入等待状态,
//而消费者有可能只进行了一次消费,这时候会唤醒所有在等待的线程,如果
//被生产者1抢到了锁,在进行一次生产后,要么会再次进行生产从而进入等待
//状态要么会放开锁重新参与竞争,这时候如果是另一个已经经过判断的生产
//者抢占到了CPU那么他会在已经满员的情况下进行生产,造成线程安全的问题。
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
linkedList.add(o);
count++;
this.notifyAll();
//为什么要唤醒所有线程?
//因为因为如果只唤醒一个线程,在消费者全部进入等待状态后,生产者
//进行生产后,如果打胜了生产者相互抢占CPU的情况,就会导致两个生
//产者同时进入等待状态,形成死锁。
}
public synchronized V get(){
V o = null;
while(linkedList.size()==0){
try {
this.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
o=linkedList.removeFirst();
count--;
this.notifyAll();
return o;
}
public static void main(String[] args) throws InterruptedException {
T<String> t = new T<>();
for (int i = 0; i <10 ; i++) {
new Thread(() -> {
while(true) {
t.get();
System.out.println("消费者"+Thread.currentThread().getName());
}
},"C"+i).start();
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i <2 ; i++) {
new Thread(new Runnable() {
@Override
public void run() {
while(true) {
t.put(Thread.currentThread().getName());
System.out.println("生产者"+Thread.currentThread().getName());
}
}
},"P"+i).start();
}
}
}
上面这种是最常见的生产者消费者模型的写法,下面我们来应用API中的Lock和Condition来精准的控制每次唤醒的是消费者还是生产者。
这种方式在生产之后会唤醒消费者,而不会唤醒另一个生产者,也是就是说这种模型更容易发生吃完了才生产,这种情况。
public class T <V>{
volatile private LinkedList<V> linkedList = new LinkedList<>();
final private int MAX = 10;
private int count = 0 ;
Lock lock = new ReentrantLock();
private Condition p = lock.newCondition();//代表生产者信号
private Condition c = lock.newCondition();//代表消费者信号
public void put(V o){
try {
lock.lock();
while(linkedList.size()==MAX){
p.await();//消费者等待
}
linkedList.add(o);
count++;
c.signalAll();//通知生产者
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
}
public synchronized V get(){
V o = null;
try {
lock.lock();
while(linkedList.size()==0){
c.await();//消费者等待
}
o=linkedList.removeFirst();
count--;
p.signalAll();//通知生产者
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
lock.unlock();
}
return o;
}
public static void main(String[] args) throws InterruptedException {
T<String> t = new T<>();
for (int i = 0; i <10 ; i++) {
new Thread(() -> {
while(true) {
t.get();
System.out.println("消费者"+Thread.currentThread().getName());
}
},"C"+i).start();
}
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
for (int i = 0; i <2 ; i++) {
new Thread(new Runnable() {
@Override
public void run() {
while(true) {
t.put(Thread.currentThread().getName());
System.out.println("生产者"+Thread.currentThread().getName());
}
}
},"P"+i).start();
}
}
}
本文深入探讨了生产者消费者模型的实现方式,包括使用synchronized关键字和Lock与Condition结合的两种方法,解释了如何避免线程安全问题和死锁情况,以及如何精确控制线程的唤醒。
&spm=1001.2101.3001.5002&articleId=104493055&d=1&t=3&u=54418fa7ade14293915b089f40f9999b)
1591

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



