import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ForkJoinPool; public class ConcurrentHashMap1 { public static void main(String[] args) {
System.out.println("Parallelism: " + ForkJoinPool.getCommonPoolParallelism()); testForEach();
testSearch();
testReduce();
} private static void testReduce() {
ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
map.putIfAbsent("foo", "bar");
map.putIfAbsent("han", "solo");
map.putIfAbsent("r2", "d2");
map.putIfAbsent("c3", "p0"); String reduced = map.reduce(1, (key, value) -> key + "=" + value,
(s1, s2) -> s1 + ", " + s2); System.out.println(reduced);
} private static void testSearch() {
ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
map.putIfAbsent("foo", "bar");
map.putIfAbsent("han", "solo");
map.putIfAbsent("r2", "d2");
map.putIfAbsent("c3", "p0"); System.out.println("\nsearch()\n"); String result1 = map.search(1, (key, value) -> {
System.out.println(Thread.currentThread().getName());
if (key.equals("foo") && value.equals("bar")) {
return "foobar";
}
return null;
}); System.out.println(result1); System.out.println("\nsearchValues()\n"); String result2 = map.searchValues(1, value -> {
System.out.println(Thread.currentThread().getName());
if (value.length() > 3) {
return value;
}
return null;
}); System.out.println(result2);
} private static void testForEach() {
ConcurrentHashMap<String, String> map = new ConcurrentHashMap<>();
map.putIfAbsent("foo", "bar");
map.putIfAbsent("han", "solo");
map.putIfAbsent("r2", "d2");
map.putIfAbsent("c3", "p0"); map.forEach(1, (key, value) -> System.out.printf("key: %s; value: %s; thread: %s\n", key, value, Thread.currentThread().getName()));
// map.forEach(5, (key, value) -> System.out.printf("key: %s; value: %s; thread: %s\n", key, value, Thread.currentThread().getName())); System.out.println(map.mappingCount());
} }

Java8-ConcurrentHashMap的更多相关文章

  1. Java7 和 Java8 中的 ConcurrentHashMap 原理解析

    Java7 中 ConcurrentHashMap ConcurrentHashMap 和 HashMap 思路是差不多的,但是因为它支持并发操作,所以要复杂一些. 整个 ConcurrentHash ...

  2. Java7与Java8中的HashMap和ConcurrentHashMap知识点总结

    JAVA7 Java7的ConcurrentHashMap里有多把锁,每一把锁用于其中一部分数据,那么当多线程访问容器里不同数据段的数据时,线程间就不会存在锁竞争,从而可以有效的提高并发访问效率呢.这 ...

  3. Java7/8 中的 HashMap 和 ConcurrentHashMap 全解析

    Java7/8 中的 HashMap 和 ConcurrentHashMap 全解析 今天发一篇”水文”,可能很多读者都会表示不理解,不过我想把它作为并发序列文章中不可缺少的一块来介绍.本来以为花不了 ...

  4. 020-并发编程-java.util.concurrent之-jdk6/7/8中ConcurrentHashMap、HashMap分析

    一.概述 哈希表(hash table)也叫散列表,是一种非常重要的数据结构,应用场景及其丰富,许多缓存技术(比如memcached)的核心其实就是在内存中维护一张大的哈希表. 是根据关键码值(Key ...

  5. Java7/8 中 HashMap 和 ConcurrentHashMap的对比和分析

    大家可能平时用HashMap比较多,相对于ConcurrentHashMap 来说并不是很熟悉.ConcurrentHashMap 是 JDK 1.5 添加的新集合,用来保证线程安全性,提升 Map ...

  6. Java容器:HashTable, synchronizedMap与ConcurrentHashMap

    首先需要明确的是,不管使用那种Map,都不能保证公共混合调用的线程安全,只能保证单条操作的线程安全,在这一点上各Map不存在优劣. 前文中简单说过HashTable和synchronizedMap,其 ...

  7. Java7/8 HashMap ConcurrentHashMap

    网上关于 HashMap 和 ConcurrentHashMap 的文章确实不少,不过缺斤少两的文章比较多,所以才想自己也写一篇,把细节说清楚说透,尤其像 Java8 中的 ConcurrentHas ...

  8. 对Java ConcurrentHashMap的一些了解

    ①引言(为什么要使用ConcurrentHashMap) 因为多线程环境下,使用Hashmap进行put操作会引起死循环,导致CPU利用率接近100%,所以在并发情况下不能使用HashMap. Has ...

  9. HashMap与ConcurrentHashMap、HashTable

    (1)HashMap的线程不安全原因一:死循环 原因在于HashMap在多线程情况下,执行resize()进行扩容时容易造成死循环. 扩容思路为它要创建一个大小为原来两倍的数组,保证新的容量仍为2的N ...

  10. 高并发编程系列:ConcurrentHashMap的实现原理(JDK1.7和JDK1.8)

    HashMap.CurrentHashMap 的实现原理基本都是BAT面试必考内容,阿里P8架构师谈:深入探讨HashMap的底层结构.原理.扩容机制深入谈过hashmap的实现原理以及在JDK 1. ...

随机推荐

  1. json 和对象互相转换

    json 和对象互相转换 导入 Jar 包: import com.fasterxml.jackson.databind.ObjectMapper; Maven 地址: <!-- https:/ ...

  2. Snapshot Array

    Implement a SnapshotArray that supports the following interface: SnapshotArray(int length) initializ ...

  3. 一个好隐蔽的C/C++代码bug

    来自:微博@ruanyf, 一本书上说,下面的 C 语言代码可能会产生无限循环.看了半天,才意识到 Bug 在哪里. 完美解答: 数组下标越界.数组a总共有10个值,a[0]~a[9].for循环里面 ...

  4. wordpress 图片上传时发生了错误,请稍后再试 的解决办法

    前一天网站还是好好的,仅一天过后就显示图片无法上传,百思不得其解 上下百度,大多数帖子提供的解决办法对我都不适用,继续搜,最后发现一篇帖子中提到是wp-config的编码格式问题 想到昨天刚好修改了下 ...

  5. LOJ6583 ICPC World Finals 2019何以伊名始(广义后缀自动机)

    对trie建SAM,询问串倒过来跑到的节点的|right|即为答案. #include<bits/stdc++.h> using namespace std; #define ll lon ...

  6. Sql Server有主外键关系时添加、删除数据

    当表之间有主外键关系时删除数据会被约束,添加.删除失败 解决办法,我们可以先把主外键关系的检查约束给关掉 → 然后删除数据 → 之后再把约束打开 查询出关掉所有外键约束的语句 SELECT 'ALTE ...

  7. SQL查询月、天、周、年(MySql的实例对比)

    SQL Server实现 日期部分 缩写 year yy, yyyy quarter qq, q month mm, m dayofyear dy, y day dd, d week wk, ww w ...

  8. nexus 匿名用户的问题。

    为了做到安全和不浪费我们自己的服务器资源,要绝对拒绝匿名用户进行访问: 1,不允许匿名用户访问 2,禁用匿名的账号 以下是这2点的设置图. ============================== ...

  9. Django对postgresql数据库进行分组聚合查询

    action(methods=['GET'], detail=False, url_path='count') def count(self, request): """ ...

  10. 把zTree前的展开收起图标改为三角形,且只有在点击三角形图标时才展开子节点解决方案

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta ht ...