1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
| package com.tools.demo.forker;
import org.apache.commons.collections.map.HashedMap;
import java.util.*; import java.util.concurrent.BlockingQueue; import java.util.concurrent.CompletableFuture; import java.util.concurrent.Future; import java.util.concurrent.LinkedBlockingDeque; import java.util.function.Consumer; import java.util.function.Function; import java.util.stream.Stream; import java.util.stream.StreamSupport;
/** * 并发方式在同一个流上执行多种操作 */ public class StreamForker<T> { private final Stream<T> stream; private final Map<Object,Function<Stream<T>,?>> forks = new HashedMap();
public StreamForker(Stream<T> stream){ this.stream = stream; } public StreamForker<T> forker(Object key,Function<Stream<T>,?> f){ forks.put(key,f); return this; } public Results getResult(){ ForkingStreamComsumer<T> comsumer = build(); try { stream.sequential().forEach(comsumer); }finally { comsumer.finish(); } return comsumer;
}
public static interface Results{ public <R> R get(Object key); } private ForkingStreamComsumer<T> build(){ List<BlockingQueue<T>> queues = new ArrayList<>(); Map<Object,Future<?>> actions = forks.entrySet().stream().reduce( new HashMap<Object,Future<?>>(), (map,e)->{ map.put(e.getKey(),getOperationResult(queues,e.getValue())); return map; }, (m1,m2)->{ m1.putAll(m2); return m1; }); return new ForkingStreamComsumer<>(queues,actions);
}
private Future<?> getOperationResult(List<BlockingQueue<T>> queues, Function<Stream<T>, ?> f) { BlockingQueue<T> queue = new LinkedBlockingDeque<>(); queues.add(queue); Spliterator<T> spliterator = new BlockingQueueSpliterator<>(queue); Stream<T> source = StreamSupport.stream(spliterator, false); return CompletableFuture.supplyAsync(()->f.apply(source)); }
static class ForkingStreamComsumer<T> implements Consumer<T>,Results { static final Object END_OF_STREAM = new Object(); private final List<BlockingQueue<T>> queues; private final Map<Object,Future<?>> actions; ForkingStreamComsumer(List<BlockingQueue<T>> queues,Map<Object,Future<?>> actions){ this.queues=queues; this.actions=actions; } void finish(){ accept((T)END_OF_STREAM); }
@Override public <R> R get(Object key) { try { return ((Future<R>) actions.get(key)).get(); } catch (Exception e) { throw new RuntimeException(e); } }
@Override public void accept(T t) { queues.forEach(q->q.add(t));
} }
class BlockingQueueSpliterator<T> implements Spliterator<T>{ private final BlockingQueue<T> q;
public BlockingQueueSpliterator(BlockingQueue<T> q) { this.q = q; }
@Override public boolean tryAdvance(Consumer<? super T> action) { T t; while (true){ try { t = q.take(); break; }catch (Exception e){} } if(t != ForkingStreamComsumer.END_OF_STREAM){ action.accept(t); return true; } return false; }
@Override public Spliterator<T> trySplit() { return null; }
@Override public long estimateSize() { return 0; }
@Override public int characteristics() { return 0; } }
public static void main(String[] args) { }
}
|