All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.elasticsearch.common.util.concurrent.ConcurrentCollections Maven / Gradle / Ivy

There is a newer version: 8.13.4
Show newest version
/*
 * Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
 * or more contributor license agreements. Licensed under the Elastic License
 * 2.0 and the Server Side Public License, v 1; you may not use this file except
 * in compliance with, at your election, the Elastic License 2.0 or the Server
 * Side Public License, v 1.
 */

package org.elasticsearch.common.util.concurrent;

import java.util.Collections;
import java.util.Deque;
import java.util.Queue;
import java.util.Set;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentLinkedDeque;
import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.LinkedTransferQueue;


public abstract class ConcurrentCollections {

    static final int aggressiveConcurrencyLevel;

    static {
        aggressiveConcurrencyLevel = Math.max(Runtime.getRuntime().availableProcessors() * 2, 16);
    }

    /**
     * Creates a new CHM with an aggressive concurrency level, aimed at high concurrent update rate long living maps.
     */
    public static  ConcurrentMap newConcurrentMapWithAggressiveConcurrency() {
        return newConcurrentMapWithAggressiveConcurrency(16);
    }

    /**
     * Creates a new CHM with an aggressive concurrency level, aimed at high concurrent update rate long living maps.
     */
    public static  ConcurrentMap newConcurrentMapWithAggressiveConcurrency(int initalCapacity) {
        return new ConcurrentHashMap<>(initalCapacity, 0.75f, aggressiveConcurrencyLevel);
    }

    public static  ConcurrentMap newConcurrentMap() {
        return new ConcurrentHashMap<>();
    }

    /**
     * Creates a new CHM with an aggressive concurrency level, aimed at highly updateable long living maps.
     */
    public static  ConcurrentMapLong newConcurrentMapLongWithAggressiveConcurrency() {
        return new ConcurrentHashMapLong<>(ConcurrentCollections.newConcurrentMapWithAggressiveConcurrency());
    }

    public static  ConcurrentMapLong newConcurrentMapLong() {
        return new ConcurrentHashMapLong<>(ConcurrentCollections.newConcurrentMap());
    }

    public static  Set newConcurrentSet() {
        return Collections.newSetFromMap(ConcurrentCollections.newConcurrentMap());
    }

    public static  Queue newQueue() {
        return new ConcurrentLinkedQueue<>();
    }

    public static  Deque newDeque() {
        return new ConcurrentLinkedDeque<>();
    }

    public static  BlockingQueue newBlockingQueue() {
        return new LinkedTransferQueue<>();
    }

    private ConcurrentCollections() {

    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy