Java Thread Callable invokeAll(Collection> tasks, ExecutorService executorService)

Here you can find the source of invokeAll(Collection> tasks, ExecutorService executorService)

Description

Invokes and waits all tasks using threadPool, avoiding thread starvation on the way (see <a href="http://gafter.blogspot.com/2006/11/thread-pool-puzzler.html">"A Thread Pool Puzzler"</a>).

License

Apache License

Declaration

public static <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks, ExecutorService executorService)
        throws Throwable 

Method Source Code


//package com.java2s;
/*/*from   www .  j  a va 2s . co m*/
 * Copyright 2000-2014 JetBrains s.r.o.
 *
 * 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.
 */

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.*;

public class Main {
    /**
     * Invokes and waits all tasks using threadPool, avoiding thread starvation on the way
     * (see <a href="http://gafter.blogspot.com/2006/11/thread-pool-puzzler.html">"A Thread Pool Puzzler"</a>).
     */
    public static <T> List<Future<T>> invokeAll(Collection<Callable<T>> tasks, ExecutorService executorService)
            throws Throwable {
        if (executorService == null) {
            for (Callable<T> task : tasks) {
                task.call();
            }
            return null;
        }

        List<Future<T>> futures = new ArrayList<Future<T>>(tasks.size());
        boolean done = false;
        try {
            for (Callable<T> t : tasks) {
                Future<T> future = executorService.submit(t);
                futures.add(future);
            }
            // force not started futures to execute using the current thread
            for (Future f : futures) {
                ((Runnable) f).run();
            }
            for (Future f : futures) {
                try {
                    f.get();
                } catch (CancellationException ignore) {
                } catch (ExecutionException e) {
                    Throwable cause = e.getCause();
                    if (cause != null) {
                        throw cause;
                    }
                }
            }
            done = true;
        } finally {
            if (!done) {
                for (Future f : futures) {
                    f.cancel(false);
                }
            }
        }
        return futures;
    }
}

Related

  1. executeWithClassLoader(ClassLoader classLoader, Callable callable)
  2. expectException(Callable callable, Class exception)
  3. getBytes(final String string)
  4. getFuture(ExecutorService executorService, Callable callable)
  5. getRandomValueForPrimitive(Class type)
  6. invokeBulkActions(Collection> tasks)
  7. max(final double[] a, final double[] b)
  8. minScalar(final double[] a, final double scalar)
  9. powerTo(final double base, final double[] exponents)