当前位置: 首页>>代码示例>>Java>>正文


Java ConcurrentHashMap.computeIfAbsent方法代码示例

本文整理汇总了Java中java.util.concurrent.ConcurrentHashMap.computeIfAbsent方法的典型用法代码示例。如果您正苦于以下问题:Java ConcurrentHashMap.computeIfAbsent方法的具体用法?Java ConcurrentHashMap.computeIfAbsent怎么用?Java ConcurrentHashMap.computeIfAbsent使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在java.util.concurrent.ConcurrentHashMap的用法示例。


在下文中一共展示了ConcurrentHashMap.computeIfAbsent方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。

示例1: testcomputeIfAbsent_performance

import java.util.concurrent.ConcurrentHashMap; //导入方法依赖的package包/类
/**
 * Tests performance of computeIfAbsent when the element is present.
 * See JDK-8161372
 * ant -Djsr166.tckTestClass=ConcurrentHashMapTest -Djsr166.methodFilter=testcomputeIfAbsent_performance -Djsr166.expensiveTests=true tck
 */
public void testcomputeIfAbsent_performance() {
    final int mapSize = 20;
    final int iterations = expensiveTests ? (1 << 23) : mapSize * 2;
    final int threads = expensiveTests ? 10 : 2;
    final ConcurrentHashMap<Integer, Integer> map = new ConcurrentHashMap<>();
    for (int i = 0; i < mapSize; i++)
        map.put(i, i);
    final ExecutorService pool = Executors.newFixedThreadPool(2);
    try (PoolCleaner cleaner = cleaner(pool)) {
        Runnable r = new CheckedRunnable() {
            public void realRun() {
                int result = 0;
                for (int i = 0; i < iterations; i++)
                    result += map.computeIfAbsent(i % mapSize, k -> k + k);
                if (result == -42) throw new Error();
            }};
        for (int i = 0; i < threads; i++)
            pool.execute(r);
    }
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:26,代码来源:ConcurrentHashMap8Test.java

示例2: addBlankFile

import java.util.concurrent.ConcurrentHashMap; //导入方法依赖的package包/类
public void addBlankFile(BlankFileMessage blankFileMessage) {
    int clientId = blankFileMessage.getClientId();
    int fileId = blankFileMessage.getFileId();

    Client client = clients.get(clientId);
    File clientFile = getOrCreateClientFile(fileId, client);

    ConcurrentHashMap<Long, Region> clientRegions = clientFile.getRegions();
    ConcurrentHashMap<Long, Region> serverRegions = files.get(fileId).getRegions();

    byte[] emptyByteArrayRegionSize = new byte[(int) REGION_SIZE];
    byte[] slowDigestForWholeEmptyRegion = Base64.getEncoder().encode(emptyByteArrayRegionSize);

    for (Region serverRegion : serverRegions.values()) {
        long offset = serverRegion.getOffset();
        long size = serverRegion.getSize();

        Region region = clientRegions.computeIfAbsent(offset, (aLong) ->
                new Region(offset, size));
        region.setQuickDigest(0);
        if(region.getSize() == REGION_SIZE) {
            region.setSlowDigest(slowDigestForWholeEmptyRegion);
        } else {
            byte[] emptyArraySmallerSize = new byte[(int) region.getSize()];
            region.setSlowDigest(
                    Base64.getEncoder()
                            .encode(emptyArraySmallerSize));
        }
    }
}
 
开发者ID:gaganis,项目名称:odoxSync,代码行数:31,代码来源:DirectorySynchronizer.java

示例3: main

import java.util.concurrent.ConcurrentHashMap; //导入方法依赖的package包/类
public static void main(String[] args) {
    ArrayList<String> list = new ArrayList<>(Arrays.asList("I", "love", "you", "too"));

    list.removeIf(new Predicate<String>() {
        @Override
        public boolean test(String s) {
            if (s.length() < 2) {

                return true;
            }
            return false;
        }
    });
    list.removeIf(l -> l.length() < 4);
    System.out.println(JSON.toJSONString(list));
    Date date = new Date();

    ConcurrentHashMap concurrentHashMap = new ConcurrentHashMap<>();
    concurrentHashMap.put("ds", 1);
    concurrentHashMap.computeIfAbsent("fdfd", k -> k.toString());
    System.out.println(JSON.toJSONString(concurrentHashMap));
    HashMap hashMap = new HashMap<>();
    hashMap.put("fd1", 1);
    hashMap.put("fd2", 1);
    hashMap.put("fd3", 1);
    hashMap.put("fd4", "fd");
    hashMap.put("fd5", 1);
    hashMap.entrySet().stream().forEach(hash -> {
        System.out.println(hash.toString());
    });
}
 
开发者ID:wxz1211,项目名称:dooo,代码行数:32,代码来源:WashMap.java

示例4: testComputeIfAbsent

import java.util.concurrent.ConcurrentHashMap; //导入方法依赖的package包/类
/**
 * computeIfAbsent adds when the given key is not present
 */
public void testComputeIfAbsent() {
    ConcurrentHashMap map = map5();
    map.computeIfAbsent(six, x -> "Z");
    assertTrue(map.containsKey(six));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:9,代码来源:ConcurrentHashMap8Test.java

示例5: testComputeIfAbsent3

import java.util.concurrent.ConcurrentHashMap; //导入方法依赖的package包/类
/**
 * computeIfAbsent does not add if function returns null
 */
public void testComputeIfAbsent3() {
    ConcurrentHashMap map = map5();
    map.computeIfAbsent(six, x -> null);
    assertFalse(map.containsKey(six));
}
 
开发者ID:AdoptOpenJDK,项目名称:openjdk-jdk10,代码行数:9,代码来源:ConcurrentHashMap8Test.java

示例6: getClientRegion

import java.util.concurrent.ConcurrentHashMap; //导入方法依赖的package包/类
private Region getClientRegion(int clientId, int fileId, long offset) {
    Client client = clients.get(clientId);

    File clientFile = getOrCreateClientFile(fileId, client);

    Region serverRegion = files.get(fileId).getRegions().get(offset);

    ConcurrentHashMap<Long, Region> clientRegions = clientFile.getRegions();
    return clientRegions.computeIfAbsent(offset, (aLong) -> new Region(serverRegion.getOffset(), serverRegion.getSize()));
}
 
开发者ID:gaganis,项目名称:odoxSync,代码行数:11,代码来源:DirectorySynchronizer.java

示例7: addOperationToHash

import java.util.concurrent.ConcurrentHashMap; //导入方法依赖的package包/类
private void addOperationToHash(ConcurrentHashMap<String, ConcurrentLinkedDeque<Operation>> userHash,
		Operation operation) {

	ConcurrentLinkedDeque<Operation> opList = userHash.computeIfAbsent(operation.getUser(), user -> new ConcurrentLinkedDeque<>());

	opList.add(operation);

}
 
开发者ID:PacktPublishing,项目名称:Java-9-Concurrency-Cookbook-Second-Edition,代码行数:9,代码来源:HashFiller.java


注:本文中的java.util.concurrent.ConcurrentHashMap.computeIfAbsent方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。