Atomically swaps in the terminal Value and calls the given callback if not already terminated. - Java java.util.concurrent.atomic

Java examples for java.util.concurrent.atomic:AtomicReferenceFieldUpdater

Description

Atomically swaps in the terminal Value and calls the given callback if not already terminated.

Demo Code

/**/*w w w . j  a va2s  .com*/
 * 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.java2s;
import java.util.concurrent.atomic.*;
import java.util.function.*;

public class Main {
    /**
     * Atomically swaps in the terminalValue and calls the given callback if not already terminated.
     * @param updater
     * @param instance
     * @param terminalValue
     * @param onTerminate
     */
    public static <T, U> void terminate(
            AtomicReferenceFieldUpdater<T, U> updater, T instance,
            U terminalValue, Consumer<? super U> onTerminate) {
        U a = updater.get(instance);
        if (a != terminalValue) {
            a = updater.getAndSet(instance, terminalValue);
            if (a != terminalValue && a != null) {
                onTerminate.accept(a);
            }
        }
    }

    /**
     * Atomically swaps in the terminalValue and calls the given callback if not already terminated.
     * @param instance
     * @param terminalValue
     * @param onTerminate
     */
    public static <U> void terminate(AtomicReference<U> instance,
            U terminalValue, Consumer<? super U> onTerminate) {
        U a = instance.get();
        if (a != terminalValue) {
            a = instance.getAndSet(terminalValue);
            if (a != terminalValue && a != null) {
                onTerminate.accept(a);
            }
        }
    }

    /**
     * Atomically swaps in the terminal value and returns the previous value.
     * @param updater
     * @param instance
     * @param terminalValue
     * @return
     */
    public static <T, U> U terminate(
            AtomicReferenceFieldUpdater<T, U> updater, T instance,
            U terminalValue) {
        U a = updater.get(instance);
        if (a != terminalValue) {
            a = updater.getAndSet(instance, terminalValue);
        }
        return a;
    }

    /**
     * Atomically swaps in the terminal value and returns the previous value.
     * @param instance
     * @param terminalValue
     * @return
     */
    public static <U> U terminate(AtomicReference<U> instance,
            U terminalValue) {
        U a = instance.get();
        if (a != terminalValue) {
            a = instance.getAndSet(terminalValue);
        }
        return a;
    }
}

Related Tutorials