SS'
SS'

Reputation: 857

Converting an iterator into a stream and calling parallel, is the conversion done on shared thread pool threads?

So I'm using the Guava's library Streams.stream(iterator) method to convert an iterator into a stream. And then I call parallel on that stream and perform some operations.

What I'm wonder is whether the converting of the iterator into a stream is done on the shared thread pool threads or beforehand.

        Stream<MyItem> myItemStream = Streams.stream(myItemIterator);
        myItem.parallel()...

Wondering because if the iterator conversion is done on the shared thread pool threads, that poses a problem for my use case in terms of thread safety.

Thanks!

Upvotes: 1

Views: 238

Answers (1)

Hangman4358
Hangman4358

Reputation: 431

The call to Streams.stream(Iterator) is just a convenience method around a call to the java StreamSupport class:

public static <T> Stream<T> stream(Iterator<T> iterator) {
  return StreamSupport.stream(Spliterators.spliteratorUnknownSize(iterator, 0), false);
}

As such the Stream will use the common ForkJoinPool.

Upvotes: 1

Related Questions