黑马程序员技术交流社区
标题: 关于两个生产者两个消费者的运行结果——求解释 [打印本页]
作者: dicegame 时间: 2013-7-28 19:10
标题: 关于两个生产者两个消费者的运行结果——求解释
本帖最后由 杜光 于 2013-8-4 10:06 编辑
- package qbb;
- class Resource {
- private String name;
- private int no = 1;
- private boolean flag = false;
- public synchronized void produce(String name) {
- if(flag) {
- try {
- wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- this.name = name + "---" + no++;
- System.out.println(Thread.currentThread().getName() +
- "---生产者: " + this.name);
- flag = true;
- notify();
- }
- public synchronized void consume() {
- if(!flag) {
- try {
- wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- System.out.println(Thread.currentThread().getName() +
- "---消费者----: " + name);
- flag = false;
- notify();
- }
- }
- class Producer implements Runnable {
- private Resource r;
- Producer(Resource r) {
- this.r = r;
- }
- public void run() {
- while(true) {
- r.produce("商品");
- }
- }
- }
- class Consumer implements Runnable {
- private Resource r;
- Consumer(Resource r) {
- this.r = r;
- }
- public void run() {
- while(true) {
- r.consume();
- }
- }
- }
- public class ProducerConsumer {
- public static void main(String[] args) {
- Resource res = new Resource();
- Producer pro = new Producer(res);
- Consumer con = new Consumer(res);
- Thread t1 = new Thread(pro);
- Thread t2 = new Thread(pro);
- Thread t3 = new Thread(con);
- Thread t4 = new Thread(con);
- t1.start();
- t2.start();
- t3.start();
- t4.start();
- }
- }
复制代码 运行结果会出现以下片段:
Thread-2---消费者----: 商品---477219
Thread-3---消费者----: 商品---477219
Thread-2---消费者----: 商品---477219
Thread-3---消费者----: 商品---477219
Thread-2---消费者----: 商品---477219
怎么解释呢?线程间怎么切换才能出现这种结果呢?
作者: 黑马李昂 时间: 2013-8-3 23:32
- public synchronized void produce(String name) {
- if(flag) {//问题出现在这里:if判断标记,只有一次,会导致不该运行的线程运行了。出现了数据错误的情况。while判断标记,解决了线程获取执行权后,是否要运行!
- try {
- wait();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- this.name = name + "---" + no++;
- System.out.println(Thread.currentThread().getName() +
- "---生产者: " + this.name);
- flag = true;
- notify();//同时这里改成notifyAll();不然出现死锁情况
- }
复制代码 与楼主共同学习{:soso_e100:}
完整代码如下- class Resource
- {
- private String name;
- private int count = 1;
- private boolean flag = false;
- public synchronized void set(String name)
- {
- while(flag)
- try{this.wait();}catch(InterruptedException e){}
- this.name = name + count;
- count++;
- System.out.println(Thread.currentThread().getName()+"...生产者..."+this.name);
- flag = true;
- notifyAll();
- }
- public synchronized void out()// t3
- {
- while(!flag)
- try{this.wait();}catch(InterruptedException e){}
- System.out.println(Thread.currentThread().getName()+"...消费者........"+this.name);
- flag = false;
- notifyAll();
- }
- }
- class Producer implements Runnable
- {
- private Resource r;
- Producer(Resource r)
- {
- this.r = r;
- }
- public void run()
- {
- while(true)
- {
- r.set("商品");
- }
- }
- }
- class Consumer implements Runnable
- {
- private Resource r;
- Consumer(Resource r)
- {
- this.r = r;
- }
- public void run()
- {
- while(true)
- {
- r.out();
- }
- }
- }
- class ProducerConsumerDemo
- {
- public static void main(String[] args)
- {
- Resource r = new Resource();
- Producer pro = new Producer(r);
- Consumer con = new Consumer(r);
- Thread t0 = new Thread(pro);
- Thread t1 = new Thread(pro);
- Thread t2 = new Thread(con);
- Thread t3 = new Thread(con);
- t0.start();
- t1.start();
- t2.start();
- t3.start();
- }
复制代码
作者: dicegame 时间: 2013-8-3 23:36
哥们,谢谢
不过,我知道正确的代码怎么写
我就是想知道再不同步的情况下
四个线程间怎么切换才能出现上面的结果
想把这个过程搞清楚
有点找虐的意思:lol
作者: 黑马李昂 时间: 2013-8-3 23:45
线程这个东西是动态的,这个真想画图给你解释,又怕画的不明白,毕向东java基础视频第14天第26个视频有详细介绍{:soso_e112:}可以回顾回顾
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) |
黑马程序员IT技术论坛 X3.2 |