StreamOpFlag.* and OutOfMemoryError when going parall
Remi Forax
forax at univ-mlv.fr
Wed Dec 5 10:41:47 PST 2012
On 12/05/2012 07:18 PM, Christian Mallwitz wrote:
> Hi,
>
> Using lambda-8-b67-linux-i586-03_dec_2012 I'm trying to compute n (not
> necessarily the first n) prime numbers:
>
> import java.util.*;
> import java.util.function.*;
> import java.util.stream.*;
>
> public class LambdaExample3 {
>
> public static boolean isPrime(long n) {
> if (n <= 1) { return false; }
> if (n == 2) { return true; }
> for (int i = 2; i <= Math.sqrt(n) + 1; i++) { if (n % i == 0)
> return false; }
> return true;
> }
>
> public static void main(String[] args) {
>
> Stream<Long> stream =
> Streams.parallel(Streams.spliteratorUnknownSize(new Iterator<Long>() {
> private long n = 0;
> @Override public boolean hasNext() { return true; }
> @Override public Long next() { return ++n; }
> }),
> // fails with OutOfMemoryError
> // StreamOpFlag.toStreamFlags(StreamOpFlag.NOT_SIZED,
> StreamOpFlag.INITIAL_OPS_VALUE)
> // fails with OutOfMemoryError
> // StreamOpFlag.NOT_SIZED
> // works, but no speed-up to non-parallel version
> StreamOpFlag.INITIAL_OPS_VALUE
> );
>
> stream
> .filter(LambdaExample3::isPrime)
> .limit(300000)
> .forEach(l -> { /*System.out.println(l);*/ });
> }
> }
>
> I'm struggling with the StreamOpFlag parameter. What should I pick?
> INITIAL_OPS_VALUE seems to work but isn't running anything in parallel
> (at least it is not faster than the serial version). NOT_SIZED isn't
> working but failing miserably with an OutOfMemoryError. IS_PARALLEL
> is not needed because I already use parallel() - should specifying
> IS_PARALLEL and using Streams.stream() supposed to go parallel as
> well?
>
> Is the OutOfMemoryError caused by a bug? The OutOfMemoryError is
> reported from a fork/join pool thread so at least it is going
> parallel? Am I missing something?
I've trouble to understand how you expect that your code work in parallel.
>
> Thanks
> Christian
>
Rémi
More information about the lambda-dev
mailing list