1

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?

  1. Delimited tokens are read from the character input stream
  2. Tokens fed to parallel unmarshaller (one token -> one Message)
  3. Messages are reordered back to retain the original incoming order
  4. 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
bobah
  • 18,364
  • 2
  • 37
  • 70

1 Answers1

2

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));
Peter Lawrey
  • 525,659
  • 79
  • 751
  • 1,130
  • Makes sense, and with iterator interface - possible? `stream.flatMap(tokenize).parallel().forEach(unmarshal).iterator().next()`-ish – bobah Feb 11 '16 at 13:28
  • You can do `stream.parallel().flatMap(tokenize).collect(toList()).iterator()` You have to call parallel() before the bit you want to do in parallel. Note: forEach/collect is a terminating method so all the computation must complete before this returns. – Peter Lawrey Feb 11 '16 at 13:44
  • 4
    No, it is irrelevant where you place the `.parallel()`. Each stream pipeline is either parallel or sequential. – Holger Feb 11 '16 at 13:59