关于java生产者、消费者的问题

Blck宋 2017-06-29 11:13:55
今天写生产者、消费者的实例遇到一个问题,不知道有没有懂得,帮忙看一下,不胜感激。
贴一下代码
生产者:
package com.core.producer.cusotmer;

import java.util.Queue;
import java.util.Random;

public class Producer extends Thread {
private int MaxSize;
private Queue<Integer> queue;

public Producer(int maxSize, Queue<Integer> queue) {
MaxSize = maxSize;
this.queue = queue;
}

@Override
public void run() {
synchronized (queue) {
while (queue.size() < MaxSize) {
int value = new Random().nextInt();
System.out.println("生产者开始生产数据了:" + value);
queue.add(value);

}
queue.notifyAll();
try {
queue.wait();
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
}
}

消费者:

package com.core.producer.cusotmer;

import java.util.Queue;

public class Customer extends Thread {
private int MaxSize;
private Queue<Integer> queue;

public Customer(int maxSize, Queue<Integer> queue) {
MaxSize = maxSize;
this.queue = queue;
}

@Override
public void run() {
synchronized (queue) {
while(queue!=null&&queue.size()>0){
System.out.println("--------消费者开始消费数据:"+queue.remove());
}
queue.notifyAll();
try {
queue.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}


测试:

package com.core.producer.cusotmer;

import java.util.LinkedList;
import java.util.Queue;

public class Test {
public static void main(String[] args) throws InterruptedException {
int Max = 3;
Queue<Integer> queue = new LinkedList<>();
Producer producer = new Producer(Max, queue);
Customer customer = new Customer(Max, queue);
producer.start();
customer.start();
}
}



问题是:按照如上代码运行的话,只运行一次就结束。
如图:


但是,如果把生产者、消费者代码中:
queue.notifyAll();
try {
queue.wait();
} catch (InterruptedException e) {
e.printStackTrace();
}

这些代码移到while循环内部,就能一直运行,但是每次都是生产者生产一个、消费者就消费一个。如图

这样也不符合设计初衷,我是想生产者每次生产一个集合,然后消费者消费一个集合。,不知道有没有大牛能解答一下我的困惑。
...全文
255 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
lyn584238910 2017-06-29
  • 打赏
  • 举报
回复
引用 2 楼 soton_dolphin 的回复:

public class Producer extends Thread {
    private int MaxSize;
    private Queue<Integer> queue;
 
    public Producer(int maxSize, Queue<Integer> queue) {
        MaxSize = maxSize;
        this.queue = queue;
    }
 
    @Override
    public void run() {
        synchronized (queue) {
            while(true){
                if(queue.size() = MaxSize) {
                    queue.wait();
                }

                int value = new Random().nextInt();
                System.out.println("生产者开始生产数据了:" + value);
                queue.add(value);
                queue.notifyAll();
                try {
                    
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
}

改了producer, 体会一下
结果没啥区别吧···求解
soton_dolphin 2017-06-29
  • 打赏
  • 举报
回复

public class Producer extends Thread {
    private int MaxSize;
    private Queue<Integer> queue;
 
    public Producer(int maxSize, Queue<Integer> queue) {
        MaxSize = maxSize;
        this.queue = queue;
    }
 
    @Override
    public void run() {
        synchronized (queue) {
            while(true){
                if(queue.size() = MaxSize) {
                    queue.wait();
                }

                int value = new Random().nextInt();
                System.out.println("生产者开始生产数据了:" + value);
                queue.add(value);
                queue.notifyAll();
                try {
                    
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
}

改了producer, 体会一下
lyn584238910 2017-06-29
  • 打赏
  • 举报
回复
你想要的是什么效果
Blck宋 2017-06-29
  • 打赏
  • 举报
回复
引用 2 楼 soton_dolphin 的回复:

public class Producer extends Thread {
    private int MaxSize;
    private Queue<Integer> queue;
 
    public Producer(int maxSize, Queue<Integer> queue) {
        MaxSize = maxSize;
        this.queue = queue;
    }
 
    @Override
    public void run() {
        synchronized (queue) {
            while(true){
                if(queue.size() = MaxSize) {
                    queue.wait();
                }

                int value = new Random().nextInt();
                System.out.println("生产者开始生产数据了:" + value);
                queue.add(value);
                queue.notifyAll();
                try {
                    
                } catch (InterruptedException e1) {
                    e1.printStackTrace();
                }
            }
        }
    }
}

改了producer, 体会一下
恩恩,懂了谢谢,需要在生产者、消费者synchronized 模块中添加while(true)循环,保持一直这个线程一直在运行

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧