- 浏览: 984788 次
文章分类
- 全部博客 (428)
- Hadoop (2)
- HBase (1)
- ELK (1)
- ActiveMQ (13)
- Kafka (5)
- Redis (14)
- Dubbo (1)
- Memcached (5)
- Netty (56)
- Mina (34)
- NIO (51)
- JUC (53)
- Spring (13)
- Mybatis (17)
- MySQL (21)
- JDBC (12)
- C3P0 (5)
- Tomcat (13)
- SLF4J-log4j (9)
- P6Spy (4)
- Quartz (12)
- Zabbix (7)
- JAVA (9)
- Linux (15)
- HTML (9)
- Lucene (0)
- JS (2)
- WebService (1)
- Maven (4)
- Oracle&MSSQL (14)
- iText (11)
- Development Tools (8)
- UTILS (4)
- LIFE (8)
最新评论
-
Donald_Draper:
Donald_Draper 写道刘落落cici 写道能给我发一 ...
DatagramChannelImpl 解析三(多播) -
Donald_Draper:
刘落落cici 写道能给我发一份这个类的源码吗Datagram ...
DatagramChannelImpl 解析三(多播) -
lyfyouyun:
请问楼主,执行消息发送的时候,报错:Transport sch ...
ActiveMQ连接工厂、连接详解 -
ezlhq:
关于 PollArrayWrapper 状态含义猜测:参考 S ...
WindowsSelectorImpl解析一(FdMap,PollArrayWrapper) -
flyfeifei66:
打算使用xmemcache作为memcache的客户端,由于x ...
Memcached分布式客户端(Xmemcached)
/* * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. * Written by Doug Lea with assistance from members of JCP JSR-166 * Expert Group and released to the public domain, as explained at * http://creativecommons.org/publicdomain/zero/1.0/ */ package java.util.concurrent; import java.util.Map; /** * A {@link java.util.Map} providing additional atomic * <tt>putIfAbsent</tt>, <tt>remove</tt>, and <tt>replace</tt> methods. * ConcurrentMap提供了原子的操作putIfAbsent,remove,replace * <p>Memory consistency effects: As with other concurrent * collections, actions in a thread prior to placing an object into a * {@code ConcurrentMap} as a key or value * [url=package-summary.html#MemoryVisibility]<i>happen-before</i>[/url] * actions subsequent to the access or removal of that object from * the {@code ConcurrentMap} in another thread. * 内存一致性:与其他并发集合意向,线程有限执行put操,即put的操作happen-before 其他线程读操作或者移除操作。 * <p>This interface is a member of the * <a href="{@docRoot}/../technotes/guides/collections/index.html"> * Java Collections Framework</a>. * * @since 1.5 * @author Doug Lea * @param <K> the type of keys maintained by this map * @param <V> the type of mapped values */ public interface ConcurrentMap<K, V> extends Map<K, V> { /** * If the specified key is not already associated * with a value, associate it with the given value. * This is equivalent to * <pre> * if (!map.containsKey(key)) * return map.put(key, value); * else * return map.get(key);</pre> * except that the action is performed atomically. * 如果Map不包含对应的Key则,执行put的操作,否则返回旧值 * @param key key with which the specified value is to be associated * @param value value to be associated with the specified key * @return the previous value associated with the specified key, or * <tt>null</tt> if there was no mapping for the key. * (A <tt>null</tt> return can also indicate that the map * previously associated <tt>null</tt> with the key, * if the implementation supports null values.) * @throws UnsupportedOperationException if the <tt>put</tt> operation * is not supported by this map * @throws ClassCastException if the class of the specified key or value * prevents it from being stored in this map * @throws NullPointerException if the specified key or value is null, * and this map does not permit null keys or values * @throws IllegalArgumentException if some property of the specified key * or value prevents it from being stored in this map * */ V putIfAbsent(K key, V value); /** * Removes the entry for a key only if currently mapped to a given value. * This is equivalent to * <pre> * if (map.containsKey(key) && map.get(key).equals(value)) { * map.remove(key); * return true; * } else return false;</pre> * except that the action is performed atomically. * 如果Map中存在Key和value相等的Entry,则移除 * @param key key with which the specified value is associated * @param value value expected to be associated with the specified key * @return <tt>true</tt> if the value was removed * @throws UnsupportedOperationException if the <tt>remove</tt> operation * is not supported by this map * @throws ClassCastException if the key or value is of an inappropriate * type for this map * ([url=../Collection.html#optional-restrictions]optional[/url]) * @throws NullPointerException if the specified key or value is null, * and this map does not permit null keys or values * ([url=../Collection.html#optional-restrictions]optional[/url]) */ boolean remove(Object key, Object value); /** * Replaces the entry for a key only if currently mapped to a given value. * This is equivalent to * <pre> * if (map.containsKey(key) && map.get(key).equals(oldValue)) { * map.put(key, newValue); * return true; * } else return false;</pre> * except that the action is performed atomically. * 如果Map中存在Key和value相等的Entry,put的操作,更新值 * @param key key with which the specified value is associated * @param oldValue value expected to be associated with the specified key * @param newValue value to be associated with the specified key * @return <tt>true</tt> if the value was replaced * @throws UnsupportedOperationException if the <tt>put</tt> operation * is not supported by this map * @throws ClassCastException if the class of a specified key or value * prevents it from being stored in this map * @throws NullPointerException if a specified key or value is null, * and this map does not permit null keys or values * @throws IllegalArgumentException if some property of a specified key * or value prevents it from being stored in this map */ boolean replace(K key, V oldValue, V newValue); 如果Map中存在Key相等的Entry,put的操作,更新值 /** * Replaces the entry for a key only if currently mapped to some value. * This is equivalent to * <pre> * if (map.containsKey(key)) { * return map.put(key, value); * } else return null;</pre> * except that the action is performed atomically. * * @param key key with which the specified value is associated * @param value value to be associated with the specified key * @return the previous value associated with the specified key, or * <tt>null</tt> if there was no mapping for the key. * (A <tt>null</tt> return can also indicate that the map * previously associated <tt>null</tt> with the key, * if the implementation supports null values.) * @throws UnsupportedOperationException if the <tt>put</tt> operation * is not supported by this map * @throws ClassCastException if the class of the specified key or value * prevents it from being stored in this map * @throws NullPointerException if the specified key or value is null, * and this map does not permit null keys or values * @throws IllegalArgumentException if some property of the specified key * or value prevents it from being stored in this map */ V replace(K key, V value); }
ConcurrentHashMap由于是线程安全,是否可以用ConcurrentHashMap来实现单例模式能,答案是肯定的
来看具体的实例:
package juc.map; import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentMap; /** * 用ConcurrentHashMap实现单例模式 * @author donald * 2017年3月11日 * 下午6:18:09 */ public class TestInstanceConcurrentMap { private static final ConcurrentMap<String, TestInstanceConcurrentMap> map = new ConcurrentHashMap<String, TestInstanceConcurrentMap>(); private static TestInstanceConcurrentMap instance; /** * * @return */ public static TestInstanceConcurrentMap getInstance() { if (instance == null) { map.putIfAbsent("INSTANCE", new TestInstanceConcurrentMap()); instance = map.get("INSTANCE"); } return instance; } /** * */ private TestInstanceConcurrentMap() { } }
发表评论
-
Executors解析
2017-04-07 14:38 1256ThreadPoolExecutor解析一(核心线程池数量、线 ... -
ScheduledThreadPoolExecutor解析三(关闭线程池)
2017-04-06 20:52 4455ScheduledThreadPoolExecutor解析一( ... -
ScheduledThreadPoolExecutor解析二(任务调度)
2017-04-06 12:56 2127ScheduledThreadPoolExecutor解析一( ... -
ScheduledThreadPoolExecutor解析一(调度任务,任务队列)
2017-04-04 22:59 4992Executor接口的定义:http://donald-dra ... -
ThreadPoolExecutor解析四(线程池关闭)
2017-04-03 23:02 9113Executor接口的定义:http: ... -
ThreadPoolExecutor解析三(线程池执行提交任务)
2017-04-03 12:06 6087Executor接口的定义:http://donald-dra ... -
ThreadPoolExecutor解析二(线程工厂、工作线程,拒绝策略等)
2017-04-01 17:12 3041Executor接口的定义:http://donald-dra ... -
ThreadPoolExecutor解析一(核心线程池数量、线程池状态等)
2017-03-31 22:01 20521Executor接口的定义:http://donald-dra ... -
ScheduledExecutorService接口定义
2017-03-29 12:53 1511Executor接口的定义:http://donald-dra ... -
AbstractExecutorService解析
2017-03-29 08:27 1081Executor接口的定义:http: ... -
ExecutorCompletionService解析
2017-03-28 14:27 1595Executor接口的定义:http://donald-dra ... -
CompletionService接口定义
2017-03-28 12:39 1069Executor接口的定义:http://donald-dra ... -
FutureTask解析
2017-03-27 12:59 1330package java.util.concurrent; ... -
Future接口定义
2017-03-26 09:40 1198/* * Written by Doug Lea with ... -
ExecutorService接口定义
2017-03-25 22:14 1164Executor接口的定义:http://donald-dra ... -
Executor接口的定义
2017-03-24 23:24 1678package java.util.concurrent; ... -
简单测试线程池拒绝执行任务策略
2017-03-24 22:37 2031线程池多余任务的拒绝执行策略有四中,分别是直接丢弃任务Disc ... -
JAVA集合类简单综述
2017-03-23 22:51 925Queue接口定义:http://donald-draper. ... -
DelayQueue解析
2017-03-23 11:00 1737Queue接口定义:http://donald-draper. ... -
SynchronousQueue解析下-TransferQueue
2017-03-22 22:20 2140Queue接口定义:http://donald-draper. ...
相关推荐
5.Lock-free:atomic、concurrentMap.putIfAbsent、CopyOnWriteArrayList 6.关于锁的经验介绍 7.并发流程控制手段:CountDownLatch、Barrier 8.定时器:ScheduledExecutorService、大规模定时器TimerWheel 9.并发三...
这篇文章主要介绍了Python concurrent.futures模块使用实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 concurrent.futures的作用: 管理并发任务池。...
5、Lock-free: atomic、concurrentMap.putIfAbsent、CopyOnWriteArrayList☆☆☆ 6、关于锁使用的经验介绍 7、并发流程控制手段:CountDownlatch、Barrier 8、定时器: ScheduledExecutorService、大规模定时器...
5、Lock-free: atomic、concurrentMap.putIfAbsent、CopyOnWriteArrayList☆☆☆ 6、关于锁使用的经验介绍 7、并发流程控制手段:CountDownlatch、Barrier 8、定时器: ScheduledExecutorService、大规模定时器...
- **ConcurrentHashMap**:一种线程安全的哈希表实现,相比传统的`synchronized`修饰的方法或者`Collections.synchronizedMap()`更加高效。 - **CopyOnWriteArrayList/CopyOnWriteArraySet**:这两个集合类采用“写...
在这个例子中,`ConcurrentMap`是由多个`ConcurrentMapShared`组成的数组。每个`ConcurrentMapShared`代表一个分片,有自己的读写锁和内部存储`map`。 #### 初始化分段锁Map ```go func New() ConcurrentMap { m :...
先阅读一下两篇介绍HashMap的文章 你真的懂大厂面试题:HashMap吗? jdk1.7 HashMap中的致命错误:循环链表 jdk1.7 ConcurrentHashMap jdk1.7 ConcurrentHashMap数据结构 jdk1.7 ConcurrentHashMap是由一个Segment...
在上一篇文章中,我们简要介绍了并发集合类的基本概念及其重要性,并探讨了如何通过共享数据结构的方法来提高程序的并发性和吞吐量。本文将继续深入到 `java.util.concurrent` 包中的 `ConcurrentHashMap` 类,它是...
赫兹 该项目为HZ地图提供堆外存储。 这是非官方的扩展和Hazelcast公司内部HZ它将替换不支持ConcurrentHashMap与离堆HTreeMap从 。 它独立于其他非堆解决方案,并且在... private final ConcurrentMap< Data , Record
一、concurrent模块的介绍 concurrent.futures模块提供了高度封装的异步调用接口 ThreadPoolExecutor:线程池,提供异步调用 ProcessPoolExecutor:进程池,提供异步调用 ProcessPoolExecutor 和 ...
- **并发集合**:这些集合类为多线程环境提供了更高效的支持,主要包括`BlockingQueue`、`ConcurrentMap`、`ConcurrentNavigableMap`等。 - **同步器**:包括`Semaphore`、`Barrier`、`Latch`、`Exchanger`等,用于...
5. **线程安全的数据结构**:介绍线程安全容器,如std::unordered_map和std::vector的线程安全版本,以及如何在多线程环境中正确使用它们。 6. **并发算法和并发模式**:讨论线程池、工作窃取队列、并行算法(如std...
Guava 是 Google 开源的一套工具库,其中提供的 cache 模块非常方便,是一种与 ConcurrentMap 相似的缓存 Map。Guava cache 模块提供了许多有用的特性,如自动加载、缓存池大小限制、缓存项过期时间设置、缓存项移除...
6. **注意并发问题**: 如果在多线程环境中遍历Map,应考虑使用`ConcurrentMap`的并发安全遍历方法,如`forEach()`,或者使用`Collections.synchronizedMap()`使普通Map线程安全。 7. **流式处理**: 自JDK 8起,还...
Collectors.toConcurrentMap()方法可以将Stream流数据收集到ConcurrentMap中,第一个参数是key的函数,第二个参数是value的函数。例如: ```java Stream.of(studentA, studentB, studentC) .parallel() .collect...
关于这是Scala集合库中并发的trie哈希映射实现的Java端口。 它曾经是从Scala到Java的几乎逐行... 此类实现所有ConcurrentMap&Iterator方法并通过所有测试。 可以用作常规Java映射(包括ConcurrentHashMap)的直接替
private static final ConcurrentMap, Book> data; static { instance = new BookDao(); data = new ConcurrentHashMap(); // 初始化数据... } // CRUD操作方法... } ``` 在`BookDao`类中,提供了获取所有...
77、简述synchronized和java.util.concurrent.locks.Lock的异同 ? 18 78、abstract class Name { private String name; public abstract boolean isStupidName(String name) {}}这有何错误? 18 79、public class ...
1. concurrent.futures模块使用:介绍如何使用这个模块来执行异步任务。 2. Asyncio模块:介绍Python的Asyncio库,用于编写单线程并发代码。 3. 事件循环管理:解释如何使用Asyncio管理事件循环。 4. 协程管理:...