Java阻止列表实现 [英] Java Blocking List Implementation

查看:53
本文介绍了Java阻止列表实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在SO和Google上搜索了此问题的答案,但到目前为止找不到合适的解决方案.

I searched for an answer to this question on SO and Google but couldn't find a proper solution so far.

我目前正在图路由问题中的LayerManager上工作.经理负责提供和重置一组固定的层.

I'm currently working on a LayerManager in a graph routing problem. The manager is responsible for providing and resetting a fixed set of layers.

我想用阻止列表实现Consumer-Producer模式,以便只要没有可用层就可以阻止传入的路由请求.到目前为止,我只找到了阻止队列,但由于我们没有不需要FIFO,LIFO,但是随机访问队列并不能真正起作用.更精确一点,这样的事情应该是可能的:

I wanted to implement the Consumer-Producer pattern with a blocking list, so that incoming routing requests are blocked as long no free layer is available. So far I only found a blocking queue but since we don't need FIFO, LIFO but random access a queue doesn't really work. To be a little more precise, something like this should be possible:

/* this should be blocking until a layer becomes available */
public Layer getLayer(){ 

    for ( Layer layer : layers ) {
        if ( layer.isUnused() && layer.matches(request) )
            return layers.pop(layer);
    }
}

有什么办法可以做到这一点?

Is there any way to achieve this?

推荐答案

我不确定我是否正确理解了您的需求,但是您可以使用阻塞队列并将结果放入列表中.如果在列表中找不到合适的图层,请调用wait()并再次检查何时将新项从队列添加到列表中.这听起来似乎在概念上是可行的,即使下面的代码无法正确执行(我很确定这也没有完全正确地同步)

I am not quite sure I understand your need correctly, but you could consume a blocking queue and put the results into a list. If an appropriate layer is not found in the list, call wait() and check again when a new item is added to the list from the queue. This sounds like it could work conceptually, even if the code below doesn't get it right (I am quite sure this is not quite properly synchronized)

public class PredicateBlockingQueue<Product> {

private final List<Product> products = new LinkedList<Product>();
private final BlockingQueue<Product> queue;
private final Thread consumer;

public PredicateBlockingQueue(int capacity) {
    queue = new ArrayBlockingQueue<Product>(capacity);

    consumer = new Thread() {
        @Override
        public void run() {
            while(!Thread.interrupted()) {
                try {
                    products.add(queue.take());
                    synchronized(queue) {
                        queue.notifyAll();
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    };

    consumer.start();
}

public void put(Product product) throws InterruptedException {
    queue.put(product);
}

public Product take(Predicate<Product> predicate) throws InterruptedException {
    Product product;
    while((product=find(predicate))==null) {
        synchronized(queue) {
            queue.wait();
        }
    }
    return product;
}

private synchronized Product find(Predicate<Product> predicate) {
    Iterator<Product> it = products.iterator();
    while(it.hasNext()) {
        Product product = it.next();
        if(predicate.test(product)) {
            it.remove();
            return product;
        }
    }
    return null;
}

这篇关于Java阻止列表实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆