/** * Execute operation on data in parallel. * * @param executorSvc Service for parallel execution. * @param srcDatas List of data for parallelization. * @param operation Logic for execution of on each item of data. * @param <T> Type of data. * @throws IgniteCheckedException if parallel execution was failed. */ public static <T, R> Collection<R> doInParallel( ExecutorService executorSvc, Collection<T> srcDatas, IgniteThrowableConsumer<T, R> operation ) throws IgniteCheckedException, IgniteInterruptedCheckedException { return doInParallel(srcDatas.size(), executorSvc, srcDatas, operation); }
/** * Template method to test parallel execution * @param executorService ExecutorService. * @param size Size. * @param parallelism Parallelism. * @throws IgniteCheckedException Exception. */ private void testOrder(ExecutorService executorService, int size, int parallelism) throws IgniteCheckedException { List<Integer> list = new ArrayList<>(); for(int i = 0; i < size; i++) list.add(i); Collection<Integer> results = IgniteUtils.doInParallel( parallelism, executorService, list, i -> i * 2 ); assertEquals(list.size(), results.size()); final int[] i = {0}; results.forEach(new Consumer<Integer>() { @Override public void accept(Integer integer) { assertEquals(2 * list.get(i[0]), integer.intValue()); i[0]++; } }); }
/** * */ @Test public void testDoInParallel() throws Throwable { CyclicBarrier barrier = new CyclicBarrier(3); ExecutorService executorService = Executors.newFixedThreadPool(3); try { IgniteUtils.doInParallel(3, executorService, asList(1, 2, 3), i -> { try { barrier.await(1, TimeUnit.SECONDS); } catch (Exception e) { throw new IgniteCheckedException(e); } return null; } ); } finally { executorService.shutdownNow(); } }
/** * */ @Test public void testDoInParallelBatch() { CyclicBarrier barrier = new CyclicBarrier(3); ExecutorService executorService = Executors.newFixedThreadPool(3); try { IgniteUtils.doInParallel(2, executorService, asList(1, 2, 3), i -> { try { barrier.await(400, TimeUnit.MILLISECONDS); } catch (Exception e) { throw new IgniteCheckedException(e); } return null; } ); fail("Should throw timeout exception"); } catch (Exception e) { assertTrue(e.toString(), X.hasCause(e, TimeoutException.class)); } finally { executorService.shutdownNow(); } }
/** * */ @Test public void testDoInParallelException() { String expectedException = "ExpectedException"; ExecutorService executorService = Executors.newFixedThreadPool(1); try { IgniteUtils.doInParallel( 1, executorService, asList(1, 2, 3), i -> { if (Integer.valueOf(1).equals(i)) throw new IgniteCheckedException(expectedException); return null; } ); fail("Should throw ParallelExecutionException"); } catch (IgniteCheckedException e) { assertEquals(expectedException, e.getMessage()); } finally { executorService.shutdownNow(); } }
doInParallel( parallelismLvl, sharedCtx.kernalContext().getSystemExecutorService(), doInParallel( parallelismLvl, sharedCtx.kernalContext().getSystemExecutorService(),
doInParallel( parallelismLvl, sharedCtx.kernalContext().getSystemExecutorService(),
doInParallel( parallelismLvl, cctx.kernalContext().getSystemExecutorService(),