Java 8 turn on parallel() stream with boolean? [closed]

╄→гoц情女王★ 提交于 2019-12-06 14:15:24

There is no switch like this in the API itself but you can add it yourself quite easily:

public static Function<Vector, Double> getNorm(boolean parallel) {
    return v -> {
        IntStream stream = IntStream.range(0, v.getDimension());
        stream = parallel ? stream.parallel() : stream;
        return Math.sqrt(stream.mapToDouble(i -> Math.pow(v.getEntry(i), 2)).sum());
    };
};

This code simply calls parallel() or not depending on the boolean parameter.

Just wanted to post an example of what I think the API should look like in general to see what you think (I'm hoping Apache Commons Math will adopt this for 4.0, but the module will be available here meanwhile firefly-math-linear-real):

/**
 * Returns a {@link BiFunction} that multiplies the first vector {@code v1}
 * times the second vector {@code v2} .
 * 
 * Example {@code multiply().apply(v1, v2);}
 * 
 * @throws MathException
 *             Of type {@code DIMENSION_MISMATCH} if
 *             {@code v1.getDimension()} is != {@code v2.getDimension()}.
 */
public static BiFunction<Vector, Vector, Vector> multiply() {
    return VectorFunctions.multiply(false);
};

/**
 * Returns a {@link BiFunction} that multiplies the first vector {@code v1}
 * times the second vector {@code v2} .
 * 
 * Example {@code multiply(true).apply(v1, v2);}
 * 
 * @param parallel
 *            Whether to perform the multiplication in parallel.
 * 
 * @throws MathException
 *             Of type {@code DIMENSION_MISMATCH} if
 *             {@code v1.getDimension()} is != {@code v2.getDimension()}.
 */
public static BiFunction<Vector, Vector, Vector> multiply(boolean parallel) {
    return (v1, v2) -> {
        checkDimensionMismatch(v1, v2);
        IntStream stream = range(0, v1.getDimension());
        stream = parallel ? stream.parallel() : stream;
        return new Vector(stream.mapToDouble(i -> v1.getEntry(i)
                * v2.getEntry(i)).toArray());
    };
}

Thoughts?

Ole

Create two functions

one named norm (implementation as provided in question), another named parallelNorm. You could reuse code between those functions.

Let encosing class controll parallelism

Your function norm resides in some class. I would

  • make function norm non-static
  • introduced final boolean member variable named parallel in encosing class
  • initialize parallel member variable in constructor
  • function norm would use parallel or non-parallel streams, based on value of parallel member variable

Use Decorator pattern to add parallelism

like in example above, I would use non-static function. I would make one class named Norm (with implementation of norm function like above). Then I would make class ParallelNorm extends Norm that would @Override norm function with parallel implementation

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