java中是否有BlockingMap作为BlockingQueue?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20945984/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 05:28:53  来源:igfitidea点击:

Is there BlockingMap as BlockingQueue in java?

javaconcurrencyjava.util.concurrent

提问by zjffdu

I'd like to have one BlockingMap data structure which is very similar to BlockingQueue. The take method of BlockingQueue will wait there until element is available. I'd like the get method of BlockingMap to wait there until the corresponding key is available? Is this kind of data structure available that I can use ?

我想要一个与 BlockingQueue 非常相似的 BlockingMap 数据结构。BlockingQueue 的 take 方法将在那里等待,直到元素可用。我希望 BlockingMap 的 get 方法在那里等待,直到相应的键可用?我可以使用这种数据结构吗?

采纳答案by Chthonic Project

I have simply used BlockingQueue<Map.Entry<K,V>>in the past. But recently, I came across this Blocking Map for Java. Haven't used it myself, though.

我过去只是简单地使用BlockingQueue<Map.Entry<K,V>>过。但最近,我遇到了这个Java 阻塞映射。不过自己没用过。

回答by Reuben

I hope thisis what you want.

我希望就是你想要的。

public class BlockingHashMap<K,V>
extends java.lang.Object
implements BlockingMap<K,V>

get

得到

public V get(java.lang.Object key)

Returns the value to which the specified key is mapped, or null if this map contains no mapping for the key. Note that null is used as a special marker to indicate the absence of the requested key

返回指定键映射到的值,如果此映射不包含键的映射,则返回 null。请注意,null 用作特殊标记以指示所请求的密钥不存在

Specified by:

指定者:

get in interface java.util.Map<K,V>

Specified by:

指定者:

get in interface BlockingMap<K,V>

Parameters:

参数:

key - the key whose associated value is to be returned

Returns:

返回:

the value to which the specified key is mapped, or null if this map contains no mapping for the key

Throws:

抛出:

java.lang.ClassCastException - if the key is of an inappropriate type for this map
java.lang.NullPointerException - if the specified key is null and this map does not permit null keys (optional)
java.lang.IllegalStateException - if the map has been shut-down

回答by Ofri Mann

Here is an extremely simple implementation using BlockingQueue and ConcurrentHashMap:

这是一个使用 BlockingQueue 和 ConcurrentHashMap 的极其简单的实现:

public class BlockingMap<K, V> {
    private Map<K, ArrayBlockingQueue<V>> map = new ConcurrentHashMap<>();

    private BlockingQueue<V> getQueue(K key, boolean replace) {
        return map.compute(key, (k, v) -> replace || v == null ? new ArrayBlockingQueue<>(1) : v);
    }

    public void put(K key, V value) {
        getQueue(key, true).add(value);
    }

    public V get(K key) throws InterruptedException {
        return getQueue(key, false).take();
    }

    public V get(K key, long timeout, TimeUnit unit) throws InterruptedException {
        return getQueue(key, false).poll(timeout, unit);
    }
}