Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/multithreading/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
生产者消费者变量java阻塞队列_Java_Multithreading_Concurrency_Queue - Fatal编程技术网

生产者消费者变量java阻塞队列

生产者消费者变量java阻塞队列,java,multithreading,concurrency,queue,Java,Multithreading,Concurrency,Queue,我正在研究Java中生产者-消费者问题的一个变体。我有一个生产者线程来创建对象,这些对象被放入优先级阻塞队列,然后被传递到主容器控制器中,控制器是一个有界缓冲区 产生队列的原因是,当主容器具有对象a的某个百分比时,它将只接受类型B的对象,以及我们被要求查看的一些其他场景。 我很难找出代码出了什么问题,调试器只是从in.offer in InQueue和in.push in Producer中跳出来。如有任何指示或建议,将不胜感激 import java.util.concurrent.P

我正在研究Java中生产者-消费者问题的一个变体。我有一个生产者线程来创建对象,这些对象被放入优先级阻塞队列,然后被传递到主容器控制器中,控制器是一个有界缓冲区

产生队列的原因是,当主容器具有对象a的某个百分比时,它将只接受类型B的对象,以及我们被要求查看的一些其他场景。 我很难找出代码出了什么问题,调试器只是从in.offer in InQueue和in.push in Producer中跳出来。如有任何指示或建议,将不胜感激

    import java.util.concurrent.PriorityBlockingQueue;

        public class InQueue implements Runnable {

        Controller c;
        private PriorityBlockingQueue in;

        public InQueue(Controller c) {
            this.c = c;
            in = new PriorityBlockingQueue();
        }

        public void push(C c) {

            in.offer(c);
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

        public void run() {
            while (true) {
                try {
                    C temp = (C) in.take(); //will block if empty
                    c.arrive(temp);
                } catch (InterruptedException e) {} // TODO
            }
        }
    }

public class Controller {

    private BoundedBuffer buffer;
    private int used;


    Controller(int capacity) {
        this.buffer = new BoundedBuffer(capacity);
        used = 0;
    }


    public void arrive(C c) {
        try {
            buffer.put(c);
            used++;
        } catch (InterruptedException e) { } //TODO
    }

    public C depart() {
        C temp = null; //BAD IDEA?
        try {
            temp = (C)buffer.take();
            used--;
        } catch (InterruptedException e) { } //TODO
        return temp; //could be null
    }
}

由于使用泛型的方法错误,代码未编译。另一件事是没有BoundedBuffer的默认实现。下面我为下面的生产者-消费者问题(阻塞队列)做了一个有效的实现。看一看,改正你的错误

package concurrency;

import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

public class Producer<T> {
    private final BlockingQueue<T> queue;
    private final Consumer consumer;
    private static volatile boolean isShutdown;
    private final static Object lock = new Object();

    public Producer() {
        this.queue = new LinkedBlockingQueue<T>();
        this.consumer = new Consumer();
    }

    public void start() {
        consumer.start();
    }

    public void stop() {
        synchronized (lock) {
            isShutdown = true;
        }
        consumer.interrupt();
    }

    public void put(T obj) throws InterruptedException {
        synchronized (lock) {
            if (isShutdown)
                throw new IllegalStateException("Consumer Thread is not active");
        }
        queue.put(obj);
    }

    private class Consumer extends Thread {

        public void run() {
            while (true) {
                synchronized (lock) {
                    if (isShutdown)
                        break;
                }

                T t = takeItem();
                // do something with 't'
                if(t!=null)
                printItem(t);
            }
        }

        private void printItem(T t) {
            System.out.println(t);
        }

        private T takeItem() {
            try {
                return queue.take();
            } catch (InterruptedException e) {
                Thread.currentThread().interrupt();
            }
            return null;
        }
    }

    public static void main(String[] args) throws InterruptedException {

        Producer<Integer> producer = new Producer<Integer>();
        producer.start();
        for (int i = 0; i <20; i++) {
            producer.put(i);
            if (i >= 7)
                Thread.sleep(500);
        }
        producer.stop();
    }
}
包并发;
导入java.util.concurrent.BlockingQueue;
导入java.util.concurrent.LinkedBlockingQueue;
公共级制作人{
私有最终阻塞队列;
私人最终消费者;
私有静态易失性布尔值关闭;
私有最终静态对象锁=新对象();
公共制片人(){
this.queue=新建LinkedBlockingQueue();
this.consumer=新消费者();
}
公开作废开始(){
consumer.start();
}
公共停车场(){
已同步(锁定){
isShutdown=true;
}
consumer.interrupt();
}
公共void put(T obj)抛出中断异常{
已同步(锁定){
如果(isShutdown)
抛出新的IllegalStateException(“消费者线程未激活”);
}
队列放置(obj);
}
私有类使用者扩展线程{
公开募捐{
while(true){
已同步(锁定){
如果(isShutdown)
打破
}
T=takeItem();
//用“t”做点什么
如果(t!=null)
打印项目(t);
}
}
私有无效打印项(T){
系统输出打印ln(t);
}
私人物品{
试一试{
返回队列。take();
}捕捉(中断异常e){
Thread.currentThread().interrupt();
}
返回null;
}
}
公共静态void main(字符串[]args)引发InterruptedException{
生产者=新生产者();
producer.start();
对于(int i=0;i=7)
睡眠(500);
}
生产者。停止();
}
}

没有,伙计,我能做到,你所列的。我承认,仿制药确实需要整理,但我认为这不是问题所在。此外,我自己编写了有界缓冲区。设置为producer->BlockingQueue->BoundedByffer->OutBlockingQueue->consumer。