/** * Construct a new instance. * * @param updater the field updater * @param componentType the component class * @param <T> the type which contains the target field * @param <V> the array value type * @return the new instance */ public static <T, V> AtomicArray<T, V> create(AtomicReferenceFieldUpdater<T, V[]> updater, Class<V> componentType) { Assert.checkNotNullParam("updater", updater); Assert.checkNotNullParam("componentType", componentType); return new AtomicArray<>(updater, (V[]) Array.newInstance(componentType, 0)); }
/** * Add a delegate executor. * * @param executor the executor to add */ public void addExecutor(final Executor executor) { Assert.checkNotNullParam("executor", executor); synchronized (this) { executorsUpdater.add(this, executor); } }
/** * Construct a new instance. * * @param executors the initial list of executors to delegate to */ public BalancingExecutor(Executor... executors) { if (executors != null && executors.length > 0) { final Executor[] clone = executors.clone(); for (int i = 0; i < clone.length; i++) { Assert.checkNotNullArrayParam("executors", i, clone[i]); } executorsUpdater.set(this, clone); } else { executorsUpdater.clear(this); } }
/** * Clear out all delegate executors at once. Tasks will be rejected until another delegate executor is added. */ public void clear() { executorsUpdater.clear(this); }
/** * Remove a delegate executor. * * @param executor the executor to remove */ public void removeExecutor(final Executor executor) { if (executor == null) { return; } synchronized (this) { executorsUpdater.remove(this, executor, true); } } }
/** * Add a value to a sorted array. Does not check for duplicates. * * @param instance the instance holding the field * @param value the value to add * @param comparator a comparator, or {@code null} to use natural ordering */ public void add(T instance, V value, Comparator<? super V> comparator) { final AtomicReferenceFieldUpdater<T, V[]> updater = this.updater; for (;;) { final V[] oldVal = updater.get(instance); final int oldLen = oldVal.length; final int pos = insertionPoint(Arrays.binarySearch(oldVal, value, comparator)); final V[] newVal = Arrays.copyOf(oldVal, oldLen + 1); newVal[pos] = value; System.arraycopy(oldVal, pos, newVal, pos + 1, oldLen - pos); if (updater.compareAndSet(instance, oldVal, newVal)) { return; } } }
/** * Clear out all delegate executors at once. Tasks will be rejected until another delegate executor is added. */ public void clear() { executorsUpdater.clear(this); }
/** * Remove a delegate executor. * * @param executor the executor to remove */ public void removeExecutor(final Executor executor) { if (executor == null) { return; } synchronized (this) { executorsUpdater.remove(this, executor, true); } } }
/** * Add a value to a sorted array. Does not check for duplicates. * * @param instance the instance holding the field * @param value the value to add * @param comparator a comparator, or {@code null} to use natural ordering */ public void add(T instance, V value, Comparator<? super V> comparator) { final AtomicReferenceFieldUpdater<T, V[]> updater = this.updater; for (;;) { final V[] oldVal = updater.get(instance); final int oldLen = oldVal.length; final int pos = insertionPoint(Arrays.binarySearch(oldVal, value, comparator)); final V[] newVal = Arrays.copyOf(oldVal, oldLen + 1); newVal[pos] = value; System.arraycopy(oldVal, pos, newVal, pos + 1, oldLen - pos); if (updater.compareAndSet(instance, oldVal, newVal)) { return; } } }
/** * Construct a new instance. * * @param executors the initial list of executors to delegate to */ public BalancingExecutor(Executor... executors) { if (executors != null && executors.length > 0) { final Executor[] clone = executors.clone(); for (int i = 0; i < clone.length; i++) { Assert.checkNotNullArrayParam("executors", i, clone[i]); } executorsUpdater.set(this, clone); } else { executorsUpdater.clear(this); } }
/** * Construct a new instance. * * @param updater the field updater * @param creator the array creator * @param <T> the type which contains the target field * @param <V> the array value type * @return the new instance * @deprecated Use {@link #create(AtomicReferenceFieldUpdater, Object[])} or {@link #create(AtomicReferenceFieldUpdater, Class)} instead. */ @Deprecated public static <T, V> AtomicArray<T, V> create(AtomicReferenceFieldUpdater<T, V[]> updater, Creator<V> creator) { Assert.checkNotNullParam("updater", updater); Assert.checkNotNullParam("creator", creator); return new AtomicArray<>(updater, creator.create(0)); }
/** * Clear out all delegate executors at once. Tasks will be rejected until another delegate executor is added. */ public void clear() { executorsUpdater.clear(this); }
/** * Add a delegate executor. * * @param executor the executor to add */ public void addExecutor(final Executor executor) { Assert.checkNotNullParam("executor", executor); synchronized (this) { executorsUpdater.add(this, executor); } }
/** * Remove a delegate executor. * * @param executor the executor to remove */ public void removeExecutor(final Executor executor) { if (executor == null) { return; } synchronized (this) { executorsUpdater.remove(this, executor, true); } } }
/** * Add a value to a sorted array. Does not check for duplicates. * * @param instance the instance holding the field * @param value the value to add * @param comparator a comparator, or {@code null} to use natural ordering */ public void add(T instance, V value, Comparator<? super V> comparator) { final AtomicReferenceFieldUpdater<T, V[]> updater = this.updater; for (;;) { final V[] oldVal = updater.get(instance); final int oldLen = oldVal.length; final int pos = insertionPoint(Arrays.binarySearch(oldVal, value, comparator)); final V[] newVal = Arrays.copyOf(oldVal, oldLen + 1); newVal[pos] = value; System.arraycopy(oldVal, pos, newVal, pos + 1, oldLen - pos); if (updater.compareAndSet(instance, oldVal, newVal)) { return; } } }
/** * Construct a new instance. * * @param executors the initial list of executors to delegate to */ public BalancingExecutor(Executor... executors) { if (executors != null && executors.length > 0) { final Executor[] clone = executors.clone(); for (int i = 0; i < clone.length; i++) { Assert.checkNotNullArrayParam("executors", i, clone[i]); } executorsUpdater.set(this, clone); } else { executorsUpdater.clear(this); } }
/** * Construct a new instance. * * @param updater the field updater * @param emptyArray an empty array of the target type * @param <T> the type which contains the target field * @param <V> the array value type * @return the new instance */ public static <T, V> AtomicArray<T, V> create(AtomicReferenceFieldUpdater<T, V[]> updater, V[] emptyArray) { Assert.checkNotNullParam("updater", updater); Assert.checkNotNullParam("emptyArray", emptyArray); if (emptyArray.length > 0) { throw Messages.msg.arrayNotEmpty(); } return new AtomicArray<>(updater, emptyArray); }
/** * Add a delegate executor. * * @param executor the executor to add */ public void addExecutor(final Executor executor) { Assert.checkNotNullParam("executor", executor); synchronized (this) { executorsUpdater.add(this, executor); } }
/** * Construct a new instance. * * @param updater the field updater * @param componentType the component class * @param <T> the type which contains the target field * @param <V> the array value type * @return the new instance */ public static <T, V> AtomicArray<T, V> create(AtomicReferenceFieldUpdater<T, V[]> updater, Class<V> componentType) { Assert.checkNotNullParam("updater", updater); Assert.checkNotNullParam("componentType", componentType); return new AtomicArray<>(updater, (V[]) Array.newInstance(componentType, 0)); }
/** * Construct a new instance. * * @param updater the field updater * @param componentType the component class * @param <T> the type which contains the target field * @param <V> the array value type * @return the new instance */ public static <T, V> AtomicArray<T, V> create(AtomicReferenceFieldUpdater<T, V[]> updater, Class<V> componentType) { Assert.checkNotNullParam("updater", updater); Assert.checkNotNullParam("componentType", componentType); return new AtomicArray<>(updater, (V[]) Array.newInstance(componentType, 0)); }