首页 > 编程语言 > 详细

多线程--简单生产者消费者升级版

时间:2018-10-07 13:34:37      阅读:146      评论:0      收藏:0      [点我收藏+]

如果有多个生产者和多个消费者 像之前那样就会产生安全问题,例如 会打印两个生产者一个消费者 或者一个生产者两个消费者 。

技术分享图片

 


为了防止这种情况的发生(线程醒了没有去判断标记),需要将if()改为while 这样当线程有等待状态被唤醒的时候可以进行循环判断,但是又由于这样会使同一类线程全部阻塞进入等待状态,没有唤醒另一类线程
因此需要将this.notify()变为this.notifyAll(); 将唤醒另一类的线程进行执行。

package cn.zz;

class Resource2 {
private String name;
private int count;
private boolean flag = false;

public synchronized void set(String name) {
while (flag) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
this.name = name + "..." + count++;
System.out.println(Thread.currentThread().getName() + "...生产者"
+ this.name);
flag = true;
this.notifyAll();
}

public synchronized void out() {
while (!flag) {
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
System.out.println(Thread.currentThread().getName() + "...消费者..."
+ this.name);
flag = false;
this.notifyAll();
}
}

class Producer2 implements Runnable {
Resource2 res2;

public Producer2(Resource2 res2) {
this.res2 = res2;
}

@Override
public void run() {
while (true) {
res2.set("商品");
}

}

}

class Consumer2 implements Runnable {
Resource2 res2;

public Consumer2(Resource2 res2) {
this.res2 = res2;
}

@Override
public void run() {
// TODO Auto-generated method stub
while (true) {
res2.out();
}
}

}

public class ProducerAndConsumer2 {
public static void main(String[] args) {
Resource2 res2 = new Resource2();

new Thread(new Producer2(res2)).start();
new Thread(new Producer2(res2)).start();
new Thread(new Consumer2(res2)).start();
new Thread(new Consumer2(res2)).start();

}
}

技术分享图片

如上图,为改进之后运行的结果,并没有产生多个消费者对应一个生产者 或者多个生产者对应一个消费者。

 

多线程--简单生产者消费者升级版

原文:https://www.cnblogs.com/twqwe/p/9749920.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!