Java中一般接触到的集合如下图:
关于Java中并发集合有:
(1)CouncurrentHashMap
(2)CopyOnWriteArrayList
(3)LinkedBlockingQueue
(4)ArrayBlockingQueue
这些的适用场景及其实现原理是必须要掌握的。
参考:HashMap 死锁分析 http://github.thinkingbar.com/hashmap-infinite-loop/
ConcurrentHashMap的1.7与1.8的实现差别很大,可以参考文章:
(1)谈谈ConcurrentHashMap1.7和1.8的不同实现 http://www.jianshu.com/p/e694f1e868ec
(2)https://zhuanlan.zhihu.com/p/21673805
下面关于ConcurrentHashMap必须要知道的几个问题:
(1)ConcurrentHashMap的锁分段技术。
(2)ConcurrentHashMap的读是否要加锁,为什么。
(3)ConcurrentHashMap的迭代器是强一致性的迭代器还是弱一致性的迭代器。
迭代器在遍历底层数组。在遍历过程中,如果已经遍历的数组上的内容变化了,迭代器不会抛出ConcurrentModificationException异常。如果未遍历的数组上的内容发生了变化,则有可能反映到迭代过程中。这就是ConcurrentHashMap迭代器弱一致的表现
ConcurrentHashMap的结构大概如下所示。
LinkedHashMap维护着一个运行于所有条目的双重链接列表。此链接列表定义了迭代顺序,该迭代顺序可以是插入顺序(insert-order)或者是访问顺序,其中默认的迭代访问顺序就是插入顺序,即可以按插入的顺序遍历元素。基于LinkedHashMap的访问顺序的特点,可构造一个LRU(Least Recently Used)最近最少使用简单缓存。也有一些开源的缓存产品如ehcache的淘汰策略(LRU)就是在LinkedHashMap上扩展的。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
public
class
LruCache<K, V>
extends
LinkedHashMap<K, V> {
/** 最大容量 */
private
int
maxCapacity;
public
LruCache(
int
maxCapacity) {
super
(
16
,
0
.75f,
true
);
this
.maxCapacity = maxCapacity;
}
public
int
getMaxCapacity() {
return
this
.maxCapacity;
}
public
void
setMaxCapacity(
int
maxCapacity) {
this
.maxCapacity = maxCapacity;
}
/**
* 当列表中的元素个数大于指定的最大容量时,返回true,并将最老的元素删除。
*/
@Override
protected
boolean
removeEldestEntry(java.util.Map.Entry<K, V> eldest) {
if
(
super
.size() > maxCapacity) {
return
true
;
}
return
false
;
}
}
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
public
class
LruCacheTest {
public
static
void
main(String[] args) {
LruCache<String, Object> cache =
new
LruCache<String, Object>(
10
);
for
(
int
i =
1
; i <=
15
; i++) {
cache.put(i +
""
, i);
}
// 此时访问指定KEY的元素
cache.get(
"10"
);
Iterator<Entry<String, Object>> iterator = cache.entrySet().iterator();
for
(; iterator.hasNext();) {
Entry<String, Object> entry = iterator.next();
System.out.println(
"key="
+ entry.getKey() +
",value="
+ entry.getValue());
}
}
}
|
输出如下:
}
}
输出如下:
1
2
3
|