Search in sources :

Example 1 with AlwaysContinue

use of com.oath.cyclops.react.async.subscription.AlwaysContinue in project cyclops by aol.

the class Queue method jdkStream.

/**
 * Return a standard (unextended) JDK Stream connected to this Queue
 * To disconnect cleanly close the queue
 *
 * <pre>
 * {@code
 *        use queue.stream().parallel() to convert to a parallel Stream
 *  }
 * </pre>
 *
 * @param closeScalingFactor Scaling factor for Queue closed messages to propagate to connected parallel Streams.
 *              Scaling Factor may need to be high to reach all connect parallel threads.
 *
 * @return Java 8 Stream connnected to this Queue
 */
public Stream<T> jdkStream(int closeScalingFactor) {
    int cores = Runtime.getRuntime().availableProcessors();
    String par = System.getProperty("java.util.concurrent.ForkJoinPool.common.parallelism");
    int connected = par != null ? Integer.valueOf(par) : cores;
    int update = 0;
    do {
        update = listeningStreams.get() + connected * closeScalingFactor;
    } while (!listeningStreams.compareAndSet(listeningStreams.get(), update));
    return closingStream(this::get, new AlwaysContinue());
}
Also used : AlwaysContinue(com.oath.cyclops.react.async.subscription.AlwaysContinue)

Aggregations

AlwaysContinue (com.oath.cyclops.react.async.subscription.AlwaysContinue)1