Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 package edu.tum.cs.ias.knowrob.utils;
00011
00012 import java.util.List;
00013 import java.util.concurrent.Callable;
00014 import java.util.concurrent.ExecutionException;
00015 import java.util.concurrent.ExecutorService;
00016 import java.util.concurrent.Executors;
00017 import java.util.concurrent.Future;
00018
00025 public class ThreadPool {
00026
00035 public static void executeInPool(List<Callable<Void>> threads) {
00036 executeInPool(threads, -1);
00037 }
00038
00039 public static void executeInPool(List<Callable<Void>> threads, int numParallel) {
00040 ExecutorService pool;
00041 int threadNum = numParallel <= 0 ? Runtime.getRuntime().availableProcessors() * 2 : numParallel;
00042 pool = Executors.newFixedThreadPool(threadNum);
00043
00044 try {
00045 List<Future<Void>> futures = pool.invokeAll(threads);
00046 for (Future<Void> f : futures) {
00047 try {
00048 f.get();
00049
00050 } catch (ExecutionException ex) {
00051 ex.getCause().printStackTrace();
00052
00053 }
00054 }
00055 } catch (InterruptedException e) {
00056 e.printStackTrace();
00057 }
00058 threads.clear();
00059 pool.shutdown();
00060 }
00061 }