zoukankan      html  css  js  c++  java
  • Java8-ConcurrentHashMap

    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("
    search()
    ");
    
            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("
    searchValues()
    ");
    
            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
    ", key, value, Thread.currentThread().getName()));
    //        map.forEach(5, (key, value) -> System.out.printf("key: %s; value: %s; thread: %s
    ", key, value, Thread.currentThread().getName()));
    
            System.out.println(map.mappingCount());
        }
    
    }
    
  • 相关阅读:
    监督学习
    第一个应用:鸢尾花分类
    第一章 计算机系统漫游
    前言
    python批量下载验证码,用来做验证码处理
    windows下安装tesserocr
    python 爬虫之requests爬取页面图片的url,并将图片下载到本地
    electron实现透明点投的方法
    css之实现下拉框自上而下展开动画效果&&自下而上收起动画效果
    react项目中canvas之画形状(圆形,椭圆形,方形)
  • 原文地址:https://www.cnblogs.com/bilaisheng/p/10210902.html
Copyright © 2011-2022 走看看