Search in sources :

Example 81 with Future

use of java.util.concurrent.Future in project disruptor by LMAX-Exchange.

the class OneToThreePipelineQueueThroughputTest method runQueuePass.

@Override
protected long runQueuePass() throws Exception {
    CountDownLatch latch = new CountDownLatch(1);
    stepThreeQueueProcessor.reset(latch);
    Future<?>[] futures = new Future[NUM_EVENT_PROCESSORS];
    futures[0] = executor.submit(stepOneQueueProcessor);
    futures[1] = executor.submit(stepTwoQueueProcessor);
    futures[2] = executor.submit(stepThreeQueueProcessor);
    long start = System.currentTimeMillis();
    long operandTwo = OPERAND_TWO_INITIAL_VALUE;
    for (long i = 0; i < ITERATIONS; i++) {
        long[] values = new long[2];
        values[0] = i;
        values[1] = operandTwo--;
        stepOneQueue.put(values);
    }
    latch.await();
    long opsPerSecond = (ITERATIONS * 1000L) / (System.currentTimeMillis() - start);
    stepOneQueueProcessor.halt();
    stepTwoQueueProcessor.halt();
    stepThreeQueueProcessor.halt();
    for (Future<?> future : futures) {
        future.cancel(true);
    }
    failIf(expectedResult, 0);
    return opsPerSecond;
}
Also used : Future(java.util.concurrent.Future) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 82 with Future

use of java.util.concurrent.Future in project disruptor by LMAX-Exchange.

the class OneToThreeQueueThroughputTest method runQueuePass.

@Override
protected long runQueuePass() throws InterruptedException {
    CountDownLatch latch = new CountDownLatch(NUM_EVENT_PROCESSORS);
    Future<?>[] futures = new Future[NUM_EVENT_PROCESSORS];
    for (int i = 0; i < NUM_EVENT_PROCESSORS; i++) {
        queueProcessors[i].reset(latch);
        futures[i] = executor.submit(queueProcessors[i]);
    }
    long start = System.currentTimeMillis();
    for (long i = 0; i < ITERATIONS; i++) {
        final Long value = Long.valueOf(i);
        for (BlockingQueue<Long> queue : blockingQueues) {
            queue.put(value);
        }
    }
    latch.await();
    long opsPerSecond = (ITERATIONS * 1000L) / (System.currentTimeMillis() - start);
    for (int i = 0; i < NUM_EVENT_PROCESSORS; i++) {
        queueProcessors[i].halt();
        futures[i].cancel(true);
        failIf(queueProcessors[i].getValue(), -1);
    }
    return opsPerSecond;
}
Also used : Future(java.util.concurrent.Future) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 83 with Future

use of java.util.concurrent.Future in project disruptor by LMAX-Exchange.

the class ThreeToOneQueueThroughputTest method runQueuePass.

@Override
protected long runQueuePass() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    queueProcessor.reset(latch);
    Future<?>[] futures = new Future[NUM_PUBLISHERS];
    for (int i = 0; i < NUM_PUBLISHERS; i++) {
        futures[i] = executor.submit(valueQueuePublishers[i]);
    }
    Future<?> processorFuture = executor.submit(queueProcessor);
    long start = System.currentTimeMillis();
    cyclicBarrier.await();
    for (int i = 0; i < NUM_PUBLISHERS; i++) {
        futures[i].get();
    }
    latch.await();
    long opsPerSecond = (ITERATIONS * 1000L) / (System.currentTimeMillis() - start);
    queueProcessor.halt();
    processorFuture.cancel(true);
    return opsPerSecond;
}
Also used : Future(java.util.concurrent.Future) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 84 with Future

use of java.util.concurrent.Future in project disruptor by LMAX-Exchange.

the class ThreeToOneSequencedBatchThroughputTest method runDisruptorPass.

@Override
protected long runDisruptorPass() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    handler.reset(latch, batchEventProcessor.getSequence().get() + ((ITERATIONS / NUM_PUBLISHERS) * NUM_PUBLISHERS));
    Future<?>[] futures = new Future[NUM_PUBLISHERS];
    for (int i = 0; i < NUM_PUBLISHERS; i++) {
        futures[i] = executor.submit(valuePublishers[i]);
    }
    executor.submit(batchEventProcessor);
    long start = System.currentTimeMillis();
    cyclicBarrier.await();
    for (int i = 0; i < NUM_PUBLISHERS; i++) {
        futures[i].get();
    }
    latch.await();
    long opsPerSecond = (ITERATIONS * 1000L) / (System.currentTimeMillis() - start);
    batchEventProcessor.halt();
    return opsPerSecond;
}
Also used : Future(java.util.concurrent.Future) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 85 with Future

use of java.util.concurrent.Future in project Hystrix by Netflix.

the class HystrixCommand method queue.

/**
     * Used for asynchronous execution of command.
     * <p>
     * This will queue up the command on the thread pool and return an {@link Future} to get the result once it completes.
     * <p>
     * NOTE: If configured to not run in a separate thread, this will have the same effect as {@link #execute()} and will block.
     * <p>
     * We don't throw an exception but just flip to synchronous execution so code doesn't need to change in order to switch a command from running on a separate thread to the calling thread.
     * 
     * @return {@code Future<R>} Result of {@link #run()} execution or a fallback from {@link #getFallback()} if the command fails for any reason.
     * @throws HystrixRuntimeException
     *             if a fallback does not exist
     *             <p>
     *             <ul>
     *             <li>via {@code Future.get()} in {@link ExecutionException#getCause()} if a failure occurs</li>
     *             <li>or immediately if the command can not be queued (such as short-circuited, thread-pool/semaphore rejected)</li>
     *             </ul>
     * @throws HystrixBadRequestException
     *             via {@code Future.get()} in {@link ExecutionException#getCause()} if invalid arguments or state were used representing a user failure, not a system failure
     * @throws IllegalStateException
     *             if invoked more than once
     */
public Future<R> queue() {
    /*
         * The Future returned by Observable.toBlocking().toFuture() does not implement the
         * interruption of the execution thread when the "mayInterrupt" flag of Future.cancel(boolean) is set to true;
         * thus, to comply with the contract of Future, we must wrap around it.
         */
    final Future<R> delegate = toObservable().toBlocking().toFuture();
    final Future<R> f = new Future<R>() {

        @Override
        public boolean cancel(boolean mayInterruptIfRunning) {
            if (delegate.isCancelled()) {
                return false;
            }
            if (HystrixCommand.this.getProperties().executionIsolationThreadInterruptOnFutureCancel().get()) {
                /*
                     * The only valid transition here is false -> true. If there are two futures, say f1 and f2, created by this command
                     * (which is super-weird, but has never been prohibited), and calls to f1.cancel(true) and to f2.cancel(false) are
                     * issued by different threads, it's unclear about what value would be used by the time mayInterruptOnCancel is checked.
                     * The most consistent way to deal with this scenario is to say that if *any* cancellation is invoked with interruption,
                     * than that interruption request cannot be taken back.
                     */
                interruptOnFutureCancel.compareAndSet(false, mayInterruptIfRunning);
            }
            final boolean res = delegate.cancel(interruptOnFutureCancel.get());
            if (!isExecutionComplete() && interruptOnFutureCancel.get()) {
                final Thread t = executionThread.get();
                if (t != null && !t.equals(Thread.currentThread())) {
                    t.interrupt();
                }
            }
            return res;
        }

        @Override
        public boolean isCancelled() {
            return delegate.isCancelled();
        }

        @Override
        public boolean isDone() {
            return delegate.isDone();
        }

        @Override
        public R get() throws InterruptedException, ExecutionException {
            return delegate.get();
        }

        @Override
        public R get(long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException {
            return delegate.get(timeout, unit);
        }
    };
    /* special handling of error states that throw immediately */
    if (f.isDone()) {
        try {
            f.get();
            return f;
        } catch (Exception e) {
            Throwable t = decomposeException(e);
            if (t instanceof HystrixBadRequestException) {
                return f;
            } else if (t instanceof HystrixRuntimeException) {
                HystrixRuntimeException hre = (HystrixRuntimeException) t;
                switch(hre.getFailureType()) {
                    case COMMAND_EXCEPTION:
                    case TIMEOUT:
                        // we don't throw these types from queue() only from queue().get() as they are execution errors
                        return f;
                    default:
                        // these are errors we throw from queue() as they as rejection type errors
                        throw hre;
                }
            } else {
                throw Exceptions.sneakyThrow(t);
            }
        }
    }
    return f;
}
Also used : HystrixBadRequestException(com.netflix.hystrix.exception.HystrixBadRequestException) Future(java.util.concurrent.Future) TimeUnit(java.util.concurrent.TimeUnit) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) TimeoutException(java.util.concurrent.TimeoutException) HystrixRuntimeException(com.netflix.hystrix.exception.HystrixRuntimeException) ExecutionException(java.util.concurrent.ExecutionException) HystrixBadRequestException(com.netflix.hystrix.exception.HystrixBadRequestException)

Aggregations

Future (java.util.concurrent.Future)1138 ArrayList (java.util.ArrayList)479 ExecutorService (java.util.concurrent.ExecutorService)445 Test (org.junit.Test)413 ExecutionException (java.util.concurrent.ExecutionException)264 Callable (java.util.concurrent.Callable)206 IOException (java.io.IOException)177 ParallelTest (com.hazelcast.test.annotation.ParallelTest)148 QuickTest (com.hazelcast.test.annotation.QuickTest)148 HashMap (java.util.HashMap)92 List (java.util.List)84 CountDownLatch (java.util.concurrent.CountDownLatch)71 LinkedList (java.util.LinkedList)67 TimeoutException (java.util.concurrent.TimeoutException)63 HashSet (java.util.HashSet)62 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)59 Map (java.util.Map)58 ICompletableFuture (com.hazelcast.core.ICompletableFuture)57 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)53 File (java.io.File)46