Java Code Examples for java.util.concurrent.ConcurrentHashMap.computeIfAbsent()

The following are Jave code examples for showing how to use computeIfAbsent() of the java.util.concurrent.ConcurrentHashMap class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: openjdk-jdk10   File: ConcurrentHashMap8Test.java   View Source Code Vote up 7 votes
/**
 * 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);
    }
}
 
Example 2
Project: odoxSync   File: DirectorySynchronizer.java   View Source Code Vote up 6 votes
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));
        }
    }
}
 
Example 3
Project: dooo   File: WashMap.java   View Source Code Vote up 6 votes
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());
    });
}
 
Example 4
Project: openjdk-jdk10   File: ConcurrentHashMap8Test.java   View Source Code Vote up 5 votes
/**
 * computeIfAbsent adds when the given key is not present
 */
public void testComputeIfAbsent() {
    ConcurrentHashMap map = map5();
    map.computeIfAbsent(six, x -> "Z");
    assertTrue(map.containsKey(six));
}
 
Example 5
Project: openjdk-jdk10   File: ConcurrentHashMap8Test.java   View Source Code Vote up 5 votes
/**
 * computeIfAbsent does not add if function returns null
 */
public void testComputeIfAbsent3() {
    ConcurrentHashMap map = map5();
    map.computeIfAbsent(six, x -> null);
    assertFalse(map.containsKey(six));
}
 
Example 6
Project: odoxSync   File: DirectorySynchronizer.java   View Source Code Vote up 4 votes
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()));
}
 
Example 7
Project: Java-9-Concurrency-Cookbook-Second-Edition   File: HashFiller.java   View Source Code Vote up 4 votes
private void addOperationToHash(ConcurrentHashMap<String, ConcurrentLinkedDeque<Operation>> userHash,
		Operation operation) {

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

	opList.add(operation);

}