Search in sources :

Example 1 with IgniteThrowableFunction

use of org.apache.ignite.internal.util.lang.IgniteThrowableFunction in project ignite by apache.

the class IgniteUtilsSelfTest method runTask.

/**
 * @param executorService Executor service.
 */
private void runTask(ExecutorService executorService) {
    List<Integer> data = asList(0, 1, 2, 3, 4, 5, 6, 7, 8, 9);
    long threadId = Thread.currentThread().getId();
    AtomicInteger curThreadCnt = new AtomicInteger();
    Collection<Integer> res;
    // Future for avoiding fast execution in only executor threads.
    // Here we try to pass a number of tasks more that executor size,
    // but there is a case when all task will be completed after last submit return control and
    // current thread can not steal task because all task will be already finished.
    GridFutureAdapter fut = new GridFutureAdapter();
    try {
        res = U.doInParallel(10, executorService, data, new IgniteThrowableFunction<Integer, Integer>() {

            @Override
            public Integer apply(Integer cnt) {
                if (Thread.currentThread().getId() == threadId) {
                    fut.onDone();
                    curThreadCnt.incrementAndGet();
                } else {
                    try {
                        fut.get();
                    } catch (IgniteCheckedException e) {
                        throw U.convertException(e);
                    }
                }
                return -cnt;
            }
        });
    } catch (IgniteCheckedException e) {
        throw new IgniteException(e);
    }
    Assert.assertTrue(curThreadCnt.get() > 0);
    Assert.assertEquals(asList(0, -1, -2, -3, -4, -5, -6, -7, -8, -9), res);
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BigInteger(java.math.BigInteger) IgniteCheckedException(org.apache.ignite.IgniteCheckedException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) IgniteException(org.apache.ignite.IgniteException) GridFutureAdapter(org.apache.ignite.internal.util.future.GridFutureAdapter) IgniteThrowableFunction(org.apache.ignite.internal.util.lang.IgniteThrowableFunction)

Aggregations

BigInteger (java.math.BigInteger)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 IgniteCheckedException (org.apache.ignite.IgniteCheckedException)1 IgniteException (org.apache.ignite.IgniteException)1 GridFutureAdapter (org.apache.ignite.internal.util.future.GridFutureAdapter)1 IgniteThrowableFunction (org.apache.ignite.internal.util.lang.IgniteThrowableFunction)1