问题
Is it possible with Java8 stream API to create interface like Message pullNext()
plumbed on top of a delimited input stream with the logical steps as below?
- Delimited tokens are read from the character input stream
- Tokens fed to parallel unmarshaller (one token -> one Message)
- Messages are reordered back to retain the original incoming order
- Messages are pullable with aforementioned
pullNext()
Somewhat a disruptor with unmarshal stage served by concurrent pool. Similar to this, maybe (implementation of stash
on top of InputStream is the one to sort out):
Iterable<String> stash = Iterables.cycle("one", "two", "three");
Iterator<String> sink = StreamSupport.stream(stash.spliterator(), true).
.parallel().map((x)->x+"-unmarshalled").iterator();
while (sink.hasNext()) process(sink.next()); // do something with decoded message
回答1:
One problem is that the parallelStream ForkJoinPool use the current thread to contribute to the pool. This means there is no current thread free to perform this actions.
The only realistic way of doing this is to kick this off in a single threaded executor to start the parallelStream doing a forEach
writing to a fixed size BlockingQueue and the current thread could consume the results of the queue.
I suspect you would be better off re-writing your code so that pullNext
isn't required. e.g.
Iterable<String> stash = Iterables.cycle("one", "two", "three");
Iterator<String> sink = StreamSupport.stream(stash.spliterator(), true).
.parallel()
.map(x -> x + "-unmarshalled")
.forEach(x -> process(x));
来源:https://stackoverflow.com/questions/35340259/parallel-message-unmarshalling-from-a-token-delimited-input-stream-with-java8-st