ConcurrentLinkedHashMap是java.util.LinkedHashMap的一个高性能实现。主要用于软件缓存。
ConcurrentLinkedHashMap 是google团队提供的一个容器。它有什么用呢?其实它本身是对
ConcurrentHashMap的封装,可以用来实现一个基于LRU策略的缓存。详细介绍可以参见
http://code.google.com/p/concurrentlinkedhashmap
https://github.com/ben-manes/concurrentlinkedhashmap
-
-
import java.util.concurrent.ConcurrentMap;
-
-
import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap;
-
import com.googlecode.concurrentlinkedhashmap.EvictionListener;
-
import com.googlecode.concurrentlinkedhashmap.Weighers;
-
-
-
-
public static void main(String[] args) {
-
-
-
-
-
-
-
private static void test001() {
-
EvictionListener<String, String> listener =
new EvictionListener<String, String>() {
-
-
-
public void onEviction(String key, String value) {
-
System.out.println(
"Evicted key=" + key +
", value=" + value);
-
-
-
ConcurrentMap<String, String> cache =
new ConcurrentLinkedHashMap.Builder<String, String>()
-
.maximumWeightedCapacity(
10).listener(listener).build();
-
-
for (
int i =
0; i <
150; i++) {
-
-
-
cache.put(String.valueOf(j),
"nihao" + i);
-
-
-
for (Map.Entry<String, String> entry : cache.entrySet()) {
-
String key = entry.getKey();
-
String value = entry.getValue();
-
System.out.println(key +
"====" + value);
-
-
System.out.println(cache.get(
"1025"));
-
-
-
-
-
-
-
-
-
-
private static void test002() {
-
-
ConcurrentLinkedHashMap<Integer, Integer> map =
new ConcurrentLinkedHashMap.Builder<Integer, Integer>()
-
.maximumWeightedCapacity(
2).weigher(Weighers.singleton())
-
-
-
-
-
-
System.out.println(map.get(
1));
-
System.out.println(map.get(
2));
-
-