最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Elasticsearch開發(fā)AtomicArray使用示例探究

 更新時間:2023年08月07日 14:17:19   作者:codecraft  
這篇文章主要為大家介紹了Elasticsearch AtomicArray使用示例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪

本文主要研究一下Elasticsearch的AtomicArray

AtomicArray

elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/common/util/concurrent/AtomicArray.java

public class AtomicArray<E> {
    private final AtomicReferenceArray<E> array;
    private volatile List<E> nonNullList;
    public AtomicArray(int size) {
        array = new AtomicReferenceArray<>(size);
    }
    /**
     * The size of the expected results, including potential null values.
     */
    public int length() {
        return array.length();
    }
    /**
     * Sets the element at position {@code i} to the given value.
     *
     * @param i     the index
     * @param value the new value
     */
    public void set(int i, E value) {
        array.set(i, value);
        if (nonNullList != null) { // read first, lighter, and most times it will be null...
            nonNullList = null;
        }
    }
    public final void setOnce(int i, E value) {
        if (array.compareAndSet(i, null, value) == false) {
            throw new IllegalStateException("index [" + i + "] has already been set");
        }
        if (nonNullList != null) { // read first, lighter, and most times it will be null...
            nonNullList = null;
        }
    }
    /**
     * Gets the current value at position {@code i}.
     *
     * @param i the index
     * @return the current value
     */
    public E get(int i) {
        return array.get(i);
    }
    /**
     * Returns the it as a non null list.
     */
    public List<E> asList() {
        if (nonNullList == null) {
            if (array == null || array.length() == 0) {
                nonNullList = Collections.emptyList();
            } else {
                List<E> list = new ArrayList<>(array.length());
                for (int i = 0; i < array.length(); i++) {
                    E e = array.get(i);
                    if (e != null) {
                        list.add(e);
                    }
                }
                nonNullList = list;
            }
        }
        return nonNullList;
    }
    /**
     * Copies the content of the underlying atomic array to a normal one.
     */
    public E[] toArray(E[] a) {
        if (a.length != array.length()) {
            throw new ElasticsearchGenerationException("AtomicArrays can only be copied to arrays of the same size");
        }
        for (int i = 0; i < array.length(); i++) {
            a[i] = array.get(i);
        }
        return a;
    }
}

AtomicArray封裝了AtomicReferenceArray并定義了nonNullList,提供了asList方法轉換為ArrayList;

而setOnce方法則使用了AtomicReferenceArray的compareAndSet方法來實現(xiàn);

另外set及setOnce都會判斷nonNullList是否為null,不為null則重新設置為null

GroupedActionListener

elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/action/support/GroupedActionListener.java

public final class GroupedActionListener<T> implements ActionListener<T> {
    private final CountDown countDown;
    private final AtomicInteger pos = new AtomicInteger();
    private final AtomicArray<T> results;
    private final ActionListener<Collection<T>> delegate;
    private final Collection<T> defaults;
    private final AtomicReference<Exception> failure = new AtomicReference<>();
    /**
     * Creates a new listener
     * @param delegate the delegate listener
     * @param groupSize the group size
     */
    public GroupedActionListener(ActionListener<Collection<T>> delegate, int groupSize,
                                 Collection<T> defaults) {
        results = new AtomicArray<>(groupSize);
        countDown = new CountDown(groupSize);
        this.delegate = delegate;
        this.defaults = defaults;
    }
    @Override
    public void onResponse(T element) {
        results.setOnce(pos.incrementAndGet() - 1, element);
        if (countDown.countDown()) {
            if (failure.get() != null) {
                delegate.onFailure(failure.get());
            } else {
                List<T> collect = this.results.asList();
                collect.addAll(defaults);
                delegate.onResponse(Collections.unmodifiableList(collect));
            }
        }
    }
    @Override
    public void onFailure(Exception e) {
        if (failure.compareAndSet(null, e) == false) {
            failure.accumulateAndGet(e, (previous, current) -> {
                previous.addSuppressed(current);
                return previous;
            });
        }
        if (countDown.countDown()) {
            delegate.onFailure(failure.get());
        }
    }
}
  • GroupedActionListener的構造器根據(jù)groupSize創(chuàng)建了AtomicArray及CountDown
  • onResponse方法會調用AtomicArray的setOnce方法來設置結果,之后判斷countDown是否都完成了,完成的話判斷是否有failure,有則回調delegate.onFailure,沒有failure則調用AtomicArray的asList方法獲取list形式的結果,最后回調delegate.onResponse
  • onFailure方法會更新failure,如果compareAndSet失敗則使用accumulateAndGet來更新,之后判斷countDown是否都完成了,完成的話則回調delegate.onFailure

CountDown

elasticsearch-7.0.1/server/src/main/java/org/elasticsearch/common/util/concurrent/CountDown.java

public final class CountDown {
    private final AtomicInteger countDown;
    private final int originalCount;
    public CountDown(int count) {
        if (count < 0) {
            throw new IllegalArgumentException("count must be greater or equal to 0 but was: " + count);
        }
        this.originalCount = count;
        this.countDown = new AtomicInteger(count);
    }
    /**
     * Decrements the count-down and returns <code>true</code> iff this call
     * reached zero otherwise <code>false</code>
     */
    public boolean countDown() {
        assert originalCount > 0;
        for (;;) {
            final int current = countDown.get();
            assert current >= 0;
            if (current == 0) {
                return false;
            }
            if (countDown.compareAndSet(current, current - 1)) {
                return current == 1;
            }
        }
    }
    /**
     * Fast forwards the count-down to zero and returns <code>true</code> iff
     * the count down reached zero with this fast forward call otherwise
     * <code>false</code>
     */
    public boolean fastForward() {
        assert originalCount > 0;
        assert countDown.get() >= 0;
        return countDown.getAndSet(0) > 0;
    }
    /**
     * Returns <code>true</code> iff the count-down has reached zero. Otherwise <code>false</code>
     */
    public boolean isCountedDown() {
        assert countDown.get() >= 0;
        return countDown.get() == 0;
    }
}

CountDown是一個簡易線程安全非阻塞版的CountDownLatch,它提供了countDown方法使用compareAndSet來遞減值,同時返回countDown是否完成(countDown.get() == 0);

另外還提供了isCountedDown來查詢countDown是否完成;還有fastForward方法用于將countDown直接設置為0

小結

  • AtomicArray封裝了AtomicReferenceArray并定義了nonNullList,提供了asList方法轉換為ArrayList;而setOnce方法則使用了AtomicReferenceArray的compareAndSet方法來實現(xiàn);
  • 另外set及setOnce都會判斷nonNullList是否為null,不為null則重新設置為null
  • GroupedActionListener的構造器根據(jù)groupSize創(chuàng)建了AtomicArray及CountDown;
  • onResponse方法會調用AtomicArray的setOnce方法來設置結果,之后判斷countDown是否都完成了,完成的話判斷是否有failure,有則回調delegate.onFailure,沒有failure則調用AtomicArray的asList方法獲取list形式的結果,最后回調delegate.onResponse;
  • onFailure方法會更新failure,如果compareAndSet失敗則使用accumulateAndGet來更新,之后判斷countDown是否都完成了,完成的話則回調delegate.onFailure
  • CountDown是一個簡易線程安全非阻塞版的CountDownLatch,它提供了countDown方法使用compareAndSet來遞減值,同時返回countDown是否完成(countDown.get() == 0);
  • 另外還提供了isCountedDown來查詢countDown是否完成;還有fastForward方法用于將countDown直接設置為0

doc

以上就是Elasticsearch AtomicArray使用示例探究的詳細內容,更多關于Elasticsearch AtomicArray的資料請關注腳本之家其它相關文章!

相關文章

  • JAVA 多態(tài) 由淺及深介紹

    JAVA 多態(tài) 由淺及深介紹

    JAVA 多態(tài) 由淺及深介紹,什么是多態(tài)?多態(tài)的詳細解釋,多態(tài)的好處,多態(tài)的實際運用等
    2013-03-03
  • springboot實現(xiàn)excel表格導出幾種常見方法

    springboot實現(xiàn)excel表格導出幾種常見方法

    在日常的開發(fā)中避免不了操作Excel,下面這篇文章主要給大家介紹了關于springboot實現(xiàn)excel表格導出的幾種常見方法,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2023-11-11
  • Java圖片裁剪和生成縮略圖的實例方法

    Java圖片裁剪和生成縮略圖的實例方法

    這篇文章主要介紹了Java圖片裁剪和生成縮略圖的實例方法,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2017-01-01
  • 淺談java繼承中是否創(chuàng)建父類對象

    淺談java繼承中是否創(chuàng)建父類對象

    下面小編就為大家?guī)硪黄獪\談java繼承中是否創(chuàng)建父類對象。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-06-06
  • SpringAMQP消息隊列(SpringBoot集成RabbitMQ方式)

    SpringAMQP消息隊列(SpringBoot集成RabbitMQ方式)

    這篇文章主要介紹了SpringAMQP消息隊列(SpringBoot集成RabbitMQ方式),具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2024-04-04
  • Spring核心思想之淺談IoC容器與依賴倒置(DI)

    Spring核心思想之淺談IoC容器與依賴倒置(DI)

    文章介紹了Spring的IoC和DI機制,以及MyBatis的動態(tài)代理,通過注解和反射,Spring能夠自動管理對象的創(chuàng)建和依賴注入,而MyBatis則通過動態(tài)代理實現(xiàn)了接口方法到數(shù)據(jù)庫操作的映射,文章詳細解釋了Spring和MyBatis的工作原理,并通過示例代碼展示了它們的結合使用方式
    2025-01-01
  • Java多線程中不同條件下編寫生產消費者模型方法介紹

    Java多線程中不同條件下編寫生產消費者模型方法介紹

    這篇文章主要介紹了Java多線程中不同條件下編寫生產消費者模型方法介紹,介紹了生產消費者模型,然后分享了相關代碼示例,具有一定參考價值,需要的朋友可以了解下。
    2017-11-11
  • mybatis映射文件mapper.xml的具體寫法

    mybatis映射文件mapper.xml的具體寫法

    在開發(fā)過程中,需要開發(fā)人員配置mapper映射文件,本文主要介紹了mybatis映射文件mapper.xml的具體寫法,感興趣的可以了解一下
    2021-09-09
  • Java實現(xiàn)選擇排序算法的實例教程

    Java實現(xiàn)選擇排序算法的實例教程

    這篇文章主要介紹了Java實現(xiàn)選擇排序算法的實例教程,選擇排序的時間復雜度為О(n&sup2;),需要的朋友可以參考下
    2016-05-05
  • Java代碼注釋規(guī)范詳解

    Java代碼注釋規(guī)范詳解

    代碼附有注釋對程序開發(fā)者來說非常重要,隨著技術的發(fā)展,在項目開發(fā)過程中,必須要求程序員寫好代碼注釋,這樣有利于代碼后續(xù)的編寫和使用。下面給大家分享java代碼注釋的規(guī)范,需要的朋友參考下
    2016-02-02

最新評論

交口县| 建宁县| 织金县| 焦作市| 岑溪市| 贵定县| 雅安市| 岗巴县| 屯门区| 论坛| 临猗县| 汉沽区| 英超| 宝鸡市| 廉江市| 康平县| 永平县| 东莞市| 南京市| 金塔县| 白城市| 嵩明县| 南京市| 宁远县| 黔东| 淅川县| 玉田县| 平远县| 老河口市| 黄浦区| 屯留县| 竹山县| 巴青县| 吉首市| 乐昌市| 韩城市| 闵行区| 东宁县| 陵川县| 乐东| 呼和浩特市|