/* * Apache License * Version 2.0, January 2004 * http://www.apache.org/licenses/ * * Copyright 2013 Aurelian Tutuianu * Copyright 2014 Aurelian Tutuianu * Copyright 2015 Aurelian Tutuianu * Copyright 2016 Aurelian Tutuianu * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */ package rapaio.util; import java.util.Optional; import java.util.concurrent.Callable; import java.util.concurrent.ExecutionException; import java.util.concurrent.ForkJoinPool; import java.util.function.Consumer; import java.util.stream.IntStream; /** * fork join pool utility * <p> * Created by <a href="mailto:padreati@yahoo.com">Aurelian Tutuianu</a> on 9/23/15. */ public class FJPool { public static void runRangeParallel(int start, int end, Consumer<Integer> r) { ForkJoinPool pool = new ForkJoinPool(Runtime.getRuntime().availableProcessors()); Optional.of(pool.submit(() -> IntStream.range(start, end).parallel().forEach(r::accept))); } public static <T> Optional<T> compute(int threads, Callable<T> r) { ForkJoinPool pool = new ForkJoinPool(threads); try { return Optional.of(pool.submit(r::call).get()); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); return Optional.empty(); } } public static void run(int threads, Runnable r) { ForkJoinPool pool = new ForkJoinPool(threads); try { pool.submit(r).get(); } catch (InterruptedException | ExecutionException e) { e.printStackTrace(); } } }