Parallel message unmarshalling from a token delimited input stream with Java8 stream API

我的梦境 提交于 2020-01-03 03:14:45

问题


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

回答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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!