Java Code Examples for java.util.concurrent.atomic.AtomicLong.decrementAndGet()

The following are Jave code examples for showing how to use decrementAndGet() of the java.util.concurrent.atomic.AtomicLong class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: boohee_v5.6   File: OnSubscribeCombineLatest.java   View Source Code Vote up 6 votes
void tick() {
    AtomicLong localCounter = this.counter;
    if (localCounter.getAndIncrement() == 0) {
        int emitted = 0;
        do {
            if (this.requested.get() > 0) {
                Object o = this.buffer.poll();
                if (o != null) {
                    if (this.buffer.isCompleted(o)) {
                        this.child.onCompleted();
                    } else {
                        this.buffer.accept(o, this.child);
                        emitted++;
                        this.requested.decrementAndGet();
                    }
                }
            }
        } while (localCounter.decrementAndGet() > 0);
        if (emitted > 0) {
            for (MultiSourceRequestableSubscriber<T, R> s : this.subscribers) {
                s.requestUpTo((long) emitted);
            }
        }
    }
}
 
Example 2
Project: boohee_v5.6   File: OperatorGroupBy.java   View Source Code Vote up 6 votes
private void emitItem(GroupState<K, T> groupState, Object item) {
    Queue<Object> q = groupState.buffer;
    AtomicLong keyRequested = groupState.requested;
    REQUESTED.decrementAndGet(this);
    if (keyRequested == null || keyRequested.get() <= 0 || !(q == null || q.isEmpty())) {
        q.add(item);
        BUFFERED_COUNT.incrementAndGet(this);
        if (groupState.count.getAndIncrement() == 0) {
            pollQueue(groupState);
        }
    } else {
        nl.accept(groupState.getObserver(), item);
        if (keyRequested.get() != Long.MAX_VALUE) {
            keyRequested.decrementAndGet();
        }
    }
    requestMoreIfNecessary();
}
 
Example 3
Project: JRediClients   File: RedissonKeys.java   View Source Code Vote up 5 votes
private void checkExecution(final RPromise<Long> result, final AtomicReference<Throwable> failed,
        final AtomicLong count, final AtomicLong executed) {
    if (executed.decrementAndGet() == 0) {
        if (failed.get() != null) {
            if (count.get() > 0) {
                RedisException ex = new RedisException("" + count.get() + " keys has been deleted. But one or more nodes has an error", failed.get());
                result.tryFailure(ex);
            } else {
                result.tryFailure(failed.get());
            }
        } else {
            result.trySuccess(count.get());
        }
    }
}
 
Example 4
Project: boohee_v5.6   File: OperatorOnBackpressureDrop.java   View Source Code Vote up 5 votes
public Subscriber<? super T> call(final Subscriber<? super T> child) {
    final AtomicLong requested = new AtomicLong();
    child.setProducer(new Producer() {
        public void request(long n) {
            BackpressureUtils.getAndAddRequest(requested, n);
        }
    });
    return new Subscriber<T>(child) {
        public void onStart() {
            request(Long.MAX_VALUE);
        }

        public void onCompleted() {
            child.onCompleted();
        }

        public void onError(Throwable e) {
            child.onError(e);
        }

        public void onNext(T t) {
            if (requested.get() > 0) {
                child.onNext(t);
                requested.decrementAndGet();
            } else if (OperatorOnBackpressureDrop.this.onDrop != null) {
                OperatorOnBackpressureDrop.this.onDrop.call(t);
            }
        }
    };
}
 
Example 5
Project: boohee_v5.6   File: OperatorObserveOn.java   View Source Code Vote up 5 votes
void pollQueue() {
    int emitted = 0;
    AtomicLong localRequested = this.requested;
    AtomicLong localCounter = this.counter;
    do {
        localCounter.set(1);
        long produced = 0;
        long r = localRequested.get();
        while (!this.child.isUnsubscribed()) {
            if (this.finished) {
                Throwable error = this.error;
                if (error != null) {
                    this.queue.clear();
                    this.child.onError(error);
                    return;
                } else if (this.queue.isEmpty()) {
                    this.child.onCompleted();
                    return;
                }
            }
            if (r > 0) {
                Object o = this.queue.poll();
                if (o != null) {
                    this.child.onNext(this.on.getValue(o));
                    r--;
                    emitted++;
                    produced++;
                }
            }
            if (produced > 0 && localRequested.get() != Long.MAX_VALUE) {
                localRequested.addAndGet(-produced);
            }
        }
        return;
    } while (localCounter.decrementAndGet() > 0);
    if (emitted > 0) {
        request((long) emitted);
    }
}
 
Example 6
Project: boohee_v5.6   File: OperatorZip.java   View Source Code Vote up 4 votes
void tick() {
    Object[] observers = this.observers;
    if (observers != null && getAndIncrement() == 0) {
        int length = observers.length;
        Observer<? super R> child = this.child;
        AtomicLong requested = this.requested;
        while (true) {
            RxRingBuffer buffer;
            Object[] vs = new Object[length];
            boolean allHaveValues = true;
            for (int i = 0; i < length; i++) {
                buffer = ((InnerSubscriber) observers[i]).items;
                Object n = buffer.peek();
                if (n == null) {
                    allHaveValues = false;
                } else if (buffer.isCompleted(n)) {
                    child.onCompleted();
                    this.childSubscription.unsubscribe();
                    return;
                } else {
                    vs[i] = buffer.getValue(n);
                }
            }
            if (requested.get() > 0 && allHaveValues) {
                try {
                    child.onNext(this.zipFunction.call(vs));
                    requested.decrementAndGet();
                    this.emitted++;
                    for (Object obj : observers) {
                        buffer = ((InnerSubscriber) obj).items;
                        buffer.poll();
                        if (buffer.isCompleted(buffer.peek())) {
                            child.onCompleted();
                            this.childSubscription.unsubscribe();
                            return;
                        }
                    }
                    if (this.emitted > THRESHOLD) {
                        for (Object obj2 : observers) {
                            ((InnerSubscriber) obj2).requestMore((long) this.emitted);
                        }
                        this.emitted = 0;
                    }
                } catch (Throwable e) {
                    Exceptions.throwOrReport(e, child, vs);
                    return;
                }
            } else if (decrementAndGet() <= 0) {
                return;
            }
        }
    }
}
 
Example 7
Project: cryptotrader   File: TemplateAgent.java   View Source Code Vote up 2 votes
@VisibleForTesting
Boolean checkCreated(Context context, Key key, String id, AtomicLong retry, Duration interval) {

    Key.KeyBuilder builder = Key.build(key);

    while (true) {

        Key current = builder.build();

        if (context.getState(current) == StateType.TERMINATE) {

            log.trace("Reconciling create terminated : {}", id);

            return FALSE;

        }

        Order order = context.findOrder(current, id);

        if (order != null) {

            log.trace("Reconcile create succeeded : {}", id);

            return TRUE;

        }

        if (retry.decrementAndGet() < 0) {
            break;
        }

        try {

            Thread.sleep(interval.toMillis());

            builder.timestamp(current.getTimestamp().plus(interval));

        } catch (InterruptedException e) {

            log.trace("Reconciling create interrupted : {}", id);

            return FALSE;

        }

    }

    log.trace("Reconcile create failed : {}", id);

    return FALSE;

}
 
Example 8
Project: cryptotrader   File: TemplateAgent.java   View Source Code Vote up 2 votes
@VisibleForTesting
Boolean checkCancelled(Context context, Key key, String id, AtomicLong retry, Duration interval) {

    Key.KeyBuilder builder = Key.build(key);

    while (true) {

        Key current = builder.build();

        if (context.getState(current) == StateType.TERMINATE) {

            log.trace("Reconciling cancel terminated : {}", id);

            return FALSE;

        }

        Order order = context.findOrder(current, id);

        if (order == null || !TRUE.equals(order.getActive())) {

            log.trace("Reconcile cancel succeeded : {}", id);

            return TRUE;

        }

        if (retry.decrementAndGet() < 0) {
            break;
        }

        try {

            Thread.sleep(interval.toMillis());

            builder.timestamp(current.getTimestamp().plus(interval));

        } catch (InterruptedException e) {

            log.trace("Reconciling cancel interrupted : {}", id);

            return FALSE;

        }

    }

    log.trace("Reconcile cancel failed : {}", id);

    return FALSE;

}