ConcurrentNavigableMap

本文详细介绍了java.util.concurrent.ConcurrentNavigableMap接口的功能和用法,包括headMap(), tailMap()和subMap()等方法的使用示例。这些方法用于获取原始映射的不同视图,这些视图可以并行访问。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

The java.util.concurrent.ConcurrentNavigableMap class is a java.util.NavigableMap with support for concurrent access, and which has concurrent access enabled for its submaps. The "submaps" are the maps returned by various methods like headMap()subMap() and tailMap().

Rather than re-explain all methods found in the NavigableMap I will just look at the methods added byConcurrentNavigableMap.

headMap()

The headMap(T toKey) method returns a view of the map containing the keys which are strictly less than the given key.

If you make changes to the original map, these changes are reflected in the head map.

Here is an example illustrating the use of the headMap() method.

ConcurrentNavigableMap map = new ConcurrentSkipListMap();

map.put("1", "one");
map.put("2", "two");
map.put("3", "three");

ConcurrentNavigableMap headMap = map.headMap("2");

The headMap will point to a ConcurrentNavigableMap which only contains the key "1", since only this key is strictly less than "2".

See the JavaDoc for more specific details of how this method works, and how its overloaded versions work.

tailMap()

The tailMap(T fromKey) method returns a view of the map containing the keys which are greater than or equal to the given fromKey.

If you make changes to the original map, these changes are reflected in the tail map.

Here is an example illustrating the use of the tailMap() method:

ConcurrentNavigableMap map = new ConcurrentSkipListMap();

map.put("1", "one");
map.put("2", "two");
map.put("3", "three");

ConcurrentNavigableMap tailMap = map.tailMap("2");

The tailMap will contain the keys "2" and "3" because these two keys are greather than or equal to the given key, "2".

See the JavaDoc for more specific details of how this method works, and how its overloaded versions work.

subMap()

The subMap() method returns a view of the original map which contains all keys from (including), to (excluding) two keys given as parameters to the method. Here is an example:

ConcurrentNavigableMap map = new ConcurrentSkipListMap();

map.put("1", "one");
map.put("2", "two");
map.put("3", "three");

ConcurrentNavigableMap subMap = map.subMap("2", "3");

The returned submap contains only the key "2", because only this key is greater than or equal to "2", and smaller than "3".

More Methods

The ConcurrentNavigableMap interface contains a few more methods that might be of use. For instance:

  • descendingKeySet()
  • descendingMap()
  • navigableKeySet()

See the official JavaDoc for more information on these methods.

// 异步批量发布确认模式 public static void publishMessageAsync() throws Exception { Channel channel = RabbitMqUtils.getChannel(); channel.queueDeclare(QUEUE_NAME_ASYNC, true, false, false, null); // 开启发布确认 channel.confirmSelect(); /** * 线程安全有序的一个哈希表(map) 适用于高并发的情况下 * 1.轻松的将序号与消息进行关联 * 2.轻松的批量删除条目 只要给到序号 * 3.支持高并发(多线程) */ ConcurrentSkipListMap<Long, String> outstandingConfirms = new ConcurrentSkipListMap<>(); /** * // 消息确认成功 回调函数 * 1. deliveryTag 消息的标识 * 2. multiple 是否为批量确认 */ ConfirmCallback ackCallback = (deliveryTag, multiple) -> { //TODO 2.删除掉已经确认的消息 判断是批量的就一起清理 单个的就直接删除 if (multiple) { ConcurrentNavigableMap<Long, String> confirmed = outstandingConfirms.headMap(deliveryTag); confirmed.clear(); }else { outstandingConfirms.remove(deliveryTag); } System.out.println("确认的消息: " + deliveryTag); }; /** * // 消息确认失败 回调函数 * 1. deliveryTag 消息的标识 * 2. multiple 是否为批量确认 */ ConfirmCallback nackCallback = (deliveryTag, multiple) -> { //TODO 3. 打印一下未确认的消息都有哪些 String message = outstandingConfirms.get(deliveryTag); System.out.println("未确认的消息是: " + message + "------------未确认消息的tag是: " + deliveryTag); }; /** * 准备消息的监听器 监听哪些消息成功了 哪些消息失败了 * 1. 监听哪些消息成功了 * 2. 监听哪些消息失败了 */ channel.addConfirmListener(ackCallback, nackCallback); // 异步通知 // 记录开始时间 long begin = System.currentTimeMillis(); // 批量发送消息 for (int i = 0; i < MESSAGE_COUNT; i++) { String message = "消息" + i; channel.basicPublish("", QUEUE_NAME_ASYNC, null, message.getBytes()); //TODO 1.此处记录下所有要发送的消息 消息的总和 outstandingConfirms.put(channel.getNextPublishSeqNo(), message); } // 记录结束时间 long end = System.currentTimeMillis(); System.out.println("发布" + MESSAGE_COUNT + "条异步批量确认消息, 耗时" + (end - begin) + "ms"); }
最新发布
04-02
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值