Search in sources :

Example 1 with CompletableFuture.runAsync

use of java8.util.concurrent.CompletableFuture.runAsync in project streamsupport by stefan-zobel.

the class SubmissionPublisherTest method testMissedSignal_8187947.

/**
 * Tests scenario for
 * JDK-8187947: A race condition in SubmissionPublisher
 * cvs update -D '2017-11-25' src/main/java/util/concurrent/SubmissionPublisher.java && ant -Djsr166.expensiveTests=true -Djsr166.tckTestClass=SubmissionPublisherTest -Djsr166.methodFilter=testMissedSignal tck; cvs update -A src/main/java/util/concurrent/SubmissionPublisher.java
 */
public void testMissedSignal_8187947() throws Exception {
    final int N = expensiveTests ? (1 << 20) : (1 << 10);
    final CountDownLatch finished = new CountDownLatch(1);
    final SubmissionPublisher<Boolean> pub = new SubmissionPublisher<>();
    class Sub implements Subscriber<Boolean> {

        int received;

        public void onSubscribe(Subscription s) {
            s.request(N);
        }

        public void onNext(Boolean item) {
            if (++received == N)
                finished.countDown();
            else
                CompletableFuture.runAsync(() -> pub.submit(Boolean.TRUE));
        }

        public void onError(Throwable t) {
            throw new AssertionError(t);
        }

        public void onComplete() {
        }
    }
    pub.subscribe(new Sub());
    CompletableFuture.runAsync(() -> pub.submit(Boolean.TRUE));
    await(finished);
}
Also used : SubmissionPublisher(java8.util.concurrent.SubmissionPublisher) Subscriber(java8.util.concurrent.Flow.Subscriber) CountDownLatch(java.util.concurrent.CountDownLatch) Subscription(java8.util.concurrent.Flow.Subscription)

Example 2 with CompletableFuture.runAsync

use of java8.util.concurrent.CompletableFuture.runAsync in project streamsupport by stefan-zobel.

the class ConcurrentAssociateTest method testOnce.

private static void testOnce(final String desc, final BiConsumer<ConcurrentMap<Object, Object>, Object> associator) {
    final ConcurrentHashMap<Object, Object> m = new ConcurrentHashMap<Object, Object>();
    final CountDownLatch s = new CountDownLatch(1);
    final Supplier<Runnable> putter = new Supplier<Runnable>() {

        @Override
        public Runnable get() {
            return new Runnable() {

                @Override
                public void run() {
                    try {
                        s.await();
                    } catch (InterruptedException e) {
                    }
                    for (int i = 0; i < N; i++) {
                        Object o = new X();
                        associator.accept(m, o);
                        if (!m.containsKey(o)) {
                            throw new AssociationFailure(desc + " failed: entry does not exist");
                        }
                    }
                }
            };
        }
    };
    Stream<CompletableFuture<Void>> putters = IntStreams.range(0, availableProcessors).mapToObj(new IntFunction<Runnable>() {

        public Runnable apply(int i) {
            return putter.get();
        }
    }).map(new Function<Runnable, CompletableFuture<Void>>() {

        @Override
        public CompletableFuture<Void> apply(Runnable runnable) {
            return CompletableFuture.runAsync(runnable);
        }
    });
    CompletableFuture<Void> all = CompletableFuture.allOf(putters.toArray(new IntFunction<CompletableFuture<Void>[]>() {

        @Override
        @SuppressWarnings("unchecked")
        public CompletableFuture<Void>[] apply(int size) {
            return (CompletableFuture<Void>[]) new CompletableFuture[size];
        }
    }));
    // Trigger the runners to start
    s.countDown();
    try {
        all.join();
    } catch (CompletionException e) {
        Throwable t = e.getCause();
        if (t instanceof AssociationFailure) {
            throw (AssociationFailure) t;
        } else {
            throw e;
        }
    }
}
Also used : CountDownLatch(java.util.concurrent.CountDownLatch) CompletableFuture(java8.util.concurrent.CompletableFuture) IntFunction(java8.util.function.IntFunction) CompletionException(java8.util.concurrent.CompletionException) Supplier(java8.util.function.Supplier) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Aggregations

CountDownLatch (java.util.concurrent.CountDownLatch)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 CompletableFuture (java8.util.concurrent.CompletableFuture)1 CompletionException (java8.util.concurrent.CompletionException)1 Subscriber (java8.util.concurrent.Flow.Subscriber)1 Subscription (java8.util.concurrent.Flow.Subscription)1 SubmissionPublisher (java8.util.concurrent.SubmissionPublisher)1 IntFunction (java8.util.function.IntFunction)1 Supplier (java8.util.function.Supplier)1