Java AtomicReference addThrowable(AtomicReferenceFieldUpdater field, T instance, Throwable exception)

Here you can find the source of addThrowable(AtomicReferenceFieldUpdater field, T instance, Throwable exception)

Description

add Throwable

License

Apache License

Declaration

public static <T> boolean addThrowable(AtomicReferenceFieldUpdater<T, Throwable> field, T instance,
            Throwable exception) 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.util.concurrent.atomic.AtomicReferenceFieldUpdater;

public class Main {
    /**//www  .j a va 2s . co m
     * A singleton instance of a Throwable indicating a terminal state for exceptions,
     * don't leak this!
     */
    public static final Throwable TERMINATED = new Throwable("No further exceptions");

    public static <T> boolean addThrowable(AtomicReferenceFieldUpdater<T, Throwable> field, T instance,
            Throwable exception) {
        for (;;) {
            Throwable current = field.get(instance);

            if (current == TERMINATED) {
                return false;
            }

            Throwable update;
            if (current == null) {
                update = exception;
            } else {
                update = new Throwable("Multiple exceptions");
                update.addSuppressed(current);
                update.addSuppressed(exception);
            }

            if (field.compareAndSet(instance, current, update)) {
                return true;
            }
        }
    }
}

Related

  1. addParameter(final String key, final String value, final StringBuilder url, final AtomicReference thisSeparator, final String nextSeparator)
  2. asList(AtomicReferenceArray a)
  3. cancel(Future future, boolean mayInterruptIfRunning)
  4. cancelAll(Future[] futures, boolean mayInterruptIfRunning)
  5. expandSplineRoots(int n)