default Topic<T> broadcast(){ Queue<T> queue = QueueFactories.<T>unboundedNonBlockingQueue() .build() .withTimeout(1); Topic<T> topic = new Topic<T>(queue,QueueFactories.<T>unboundedNonBlockingQueue()); AtomicBoolean wip = new AtomicBoolean(false); Spliterator<T> split = this.spliterator(); Continuation ref[] = {null}; Continuation cont = new Continuation(()->{ if(wip.compareAndSet(false,true)){ try { //use the first consuming thread to tell this Stream onto the Queue if(!split.tryAdvance(topic::offer)){ topic.close(); return Continuation.empty(); } }finally { wip.set(false); } } return ref[0]; }); ref[0]=cont; queue.addContinuation(cont); return topic; }
.withTimeout(1); Topic<T> topic = new Topic<>(queue, QueueFactories.<T>boundedNonBlockingQueue(1000)); AtomicBoolean wip = new AtomicBoolean(false); .withTimeout(1); Topic<T> topic = new Topic<>(queue, QueueFactories.<T>boundedNonBlockingQueue(1000)); AtomicBoolean wip = new AtomicBoolean(false); Subscription s = source.subscribe(topic::offer, e -> topic.close(), () -> topic.close());
public MultipleStreamSource(final Queue<T> q) { topic = new Topic( q); }
default Topic<T> broadcast(){ Queue<T> queue = QueueFactories.<T>unboundedNonBlockingQueue() .build() .withTimeout(1); Topic<T> topic = new Topic<T>(queue,QueueFactories.<T>unboundedNonBlockingQueue()); AtomicBoolean wip = new AtomicBoolean(false); Spliterator<T> split = this.spliterator(); Continuation ref[] = {null}; Continuation cont = new Continuation(()->{ if(wip.compareAndSet(false,true)){ try { //use the first consuming thread to tell this Stream onto the Queue if(!split.tryAdvance(topic::offer)){ topic.close(); return Continuation.empty(); } }finally { wip.set(false); } } return ref[0]; }); ref[0]=cont; queue.addContinuation(cont); return topic; }
.withTimeout(1); Topic<T> topic = new Topic<>(queue, QueueFactories.<T>boundedNonBlockingQueue(1000)); AtomicBoolean wip = new AtomicBoolean(false); .withTimeout(1); Topic<T> topic = new Topic<>(queue, QueueFactories.<T>boundedNonBlockingQueue(1000)); AtomicBoolean wip = new AtomicBoolean(false); Subscription s = source.subscribe(topic::offer, e -> topic.close(), () -> topic.close());