集册 Java实例教程 原子地用一个不包含给定值的新数组替换实例中的数组,如果实例持有终端状态或空数组,或者该值不在数组中,则返回false。

原子地用一个不包含给定值的新数组替换实例中的数组,如果实例持有终端状态或空数组,或者该值不在数组中,则返回false。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

352
原子地用一个不包含给定值的新数组替换实例中的数组,如果实例持有终端状态或空数组,或者该值不在数组中,则返回false。

/**

 * Copyright 2015 David Karnok and Netflix, Inc.

 * 

 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in

 * compliance with the License. You may obtain a copy of the License at

 * 

 * http://www.apache.org/licenses/LICENSE-2.0

 * 

 * Unless required by applicable law or agreed to in writing, software distributed under the License is

 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See

 * the License for the specific language governing permissions and limitations under the License.

 */

//package com.nowjava;

import java.util.concurrent.atomic.*;

import java.util.function.*;//from 时   代    Java - nowjava.com


public class Main {

    /**

     * Atomically replaces the array in instance with a new array that doesn't contain the

     * given value or returns false if the instance holds the terminal state or an empty array

     * or the value is not in the array.

     * @param updater

     * @param instance

     * @param value

     * @param terminalValue

     * @param zeroArray

     * @param arraySupplier

     * @return

     */

    public static <T, U> boolean remove(

            AtomicReferenceFieldUpdater<T, U[]> updater, T instance,

            U value, U[] terminalValue, U[] zeroArray,

            IntFunction<U[]> arraySupplier) {

        for (;;) {

            U[] a = updater.get(instance);

            if (a == terminalValue) {

                return false;

            }

            int n = a.length;

            if (n == 0) {

                return false;

            }
            /*
            nowjava.com
            */

            int j = -1;

            for (int i = 0; i < n; i++) {

                if (a[i] == value) {

                    j = i;

                    break;

                }

            }

            if (j < 0) {

                return false;

            }

            U[] b;

            if (n == 1) {

                b = zeroArray;

            } else {

                b = arraySupplier.apply(n - 1);

                System.arraycopy(a, 0, b, 0, j);

                System.arraycopy(a, j + 1, b, j, n - j - 1);

            }

            if (updater.compareAndSet(instance, a, b)) {

                return true;

            }

        }

    }


    /**

     * Atomically replaces the array in instance with a new array that doesn't contain the

     * given value or returns false if the instance holds the terminal state or an empty array

     * or the value is not in the array.

     * @param reference

     * @param value

     * @param terminalValue

     * @param zeroArray

     * @param arraySupplier

     * @return

     */

    public static <U> boolean remove(AtomicReference<U[]> reference,

            U value, U[] terminalValue, U[] zeroArray,

            IntFunction<U[]> arraySupplier) {

        for (;;) {

            U[] a = reference.get();

            if (a == terminalValue) {

                return false;

            }

            int n = a.length;

            if (n == 0) {

                return false;

            }

            int j = -1;

            for (int i = 0; i < n; i++) {

                
展开阅读全文