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());
        }
    
    }
    
  • 相关阅读:
    70. Climbing Stairs(动态规划)
    53. Maximum Subarray(动态规划)
    PAT 1045. Favorite Color Stripe
    PAT 1044. Shopping in Mars
    分治策略
    时间复杂度和空间复杂度分析(转载)
    [转载]论坛中某位达人自己编写的Morlet连续小波变换程序
    连续小波时频图绘制原理    连续小波变换尺度与信号频率的关系
    Matlab中wavedec使用學習及詳解
    [转载]转载:小波分解层数与尺度的关系
  • 原文地址:https://www.cnblogs.com/bilaisheng/p/10210902.html
Copyright © 2011-2022 走看看