Example usage for java.util.concurrent Executors newFixedThreadPool

List of usage examples for java.util.concurrent Executors newFixedThreadPool

Introduction

In this page you can find the example usage for java.util.concurrent Executors newFixedThreadPool.

Prototype

public static ExecutorService newFixedThreadPool(int nThreads) 

Source Link

Document

Creates a thread pool that reuses a fixed number of threads operating off a shared unbounded queue.

Usage

From source file:Main.java

public static long pmax(final long[][] arr, int numThreads) {
    ExecutorService pool = Executors.newFixedThreadPool(numThreads);
    try {/*w  ww .ja v  a  2 s .co m*/
        List<Future<Long>> list = new ArrayList<Future<Long>>();
        for (int i = 0; i < arr.length; i++) {
            final long[] subArr = arr[i];
            list.add(pool.submit(new Callable<Long>() {
                public Long call() {
                    long max = Long.MIN_VALUE;
                    for (int j = 0; j < subArr.length; j++) {
                        if (subArr[j] > max) {
                            max = subArr[j];
                        }
                    }
                    return max;
                }
            }));
        }
        // find the max of each slice's max:
        long max = Long.MIN_VALUE;
        for (Future<Long> future : list) {
            long threadMax = future.get();
            System.out.println("threadMax: " + threadMax);
            if (threadMax > max) {
                max = threadMax;
            }
        }
        return max;
    } catch (Exception e) {
        System.out.println(e);
        return -1;
    } finally {
        pool.shutdown();
    }
}

From source file:Main.java

public static void runRunnablesNTimesAndAwaitCompletion(int times, final List<Runnable> runnables)
        throws InterruptedException {
    ExecutorService newFixedThreadPool = Executors.newFixedThreadPool(runnables.size());
    for (Runnable r : runnables) {
        for (int i = 0; i < times; i++) {
            newFixedThreadPool.execute(r);
        }/*from  w  w w .ja v a  2 s  .c  om*/
    }
    newFixedThreadPool.shutdown();
    newFixedThreadPool.awaitTermination(5, TimeUnit.SECONDS);
}

From source file:Main.java

/**
 * Create new executor with a thread for each processor core.
 *
 * @return A new executor with a thread for each processor core.
 *///from   ww  w  . j  a v  a2s .  c  o  m
public static ExecutorService allAvailableProcessors() {
    return Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
}

From source file:Main.java

public static void invokeBulkActions(Collection<Callable<Object>> tasks, int numFixedThreads) {
    ExecutorService executor = Executors.newFixedThreadPool(numFixedThreads);
    try {//from w  w w.  jav  a2 s. co  m
        executor.invokeAll(tasks);
    } catch (InterruptedException iex) {
    } finally {
        executor.shutdown();
    }
}

From source file:Main.java

public static <T> T getFutureResult(Callable<T> task, long timeout) throws TimeoutException {
    try {/*from   www. ja  va  2 s.  co m*/
        ExecutorService executor = Executors.newFixedThreadPool(1);
        Future<T> future = executor.submit(task);
        T result = null;
        if (timeout <= 0) {
            System.out.println("get wait forever");
            result = future.get();
        } else {
            System.out.println("get wait " + timeout);
            result = future.get(timeout, TimeUnit.MILLISECONDS);
        }
        return result;
    } catch (java.util.concurrent.TimeoutException ex) {
        throw ex;
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
}

From source file:Main.java

private static ExecutorService getExecutorService() {
    if (executorService == null) {
        executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors() * 2 + 2);
    }//from ww w  .j  a  v  a 2  s  .  c  om
    return executorService;
}

From source file:Main.java

public static final void initPool(int threadSize) {
    if (null == executorService) {
        lock.lock();/*from w  w w . j  a v  a 2  s. co  m*/
        try {
            executorService = Executors.newFixedThreadPool(threadSize);
        } finally {
            lock.unlock();
        }
    }
}

From source file:Main.java

public static boolean shutdown() {
    try {/*from   w  w  w . ja  va  2 s . c o  m*/
        executorService.shutdown();
        return executorService.awaitTermination(STANDARD_THREAD_COUNT / 100, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        e.printStackTrace();
        return true;
    } finally {
        executorService = Executors.newFixedThreadPool(6);
    }
}

From source file:Main.java

public static ExecutorService newFixedThreadPool(int minimalSize) {
    return Executors.newFixedThreadPool(threadPoolSize(minimalSize));
}

From source file:Main.java

private static ExecutorService getExecutorService(Class clazz) {
    String key = clazz.getName();
    if (!map.containsKey(key)) {
        synchronized (map) {
            if (!map.containsKey(key)) {
                map.put(key, Executors.newFixedThreadPool(TOTAL_SIZE));
            }/*from w  w  w  .j  a v  a2s . co m*/
        }
    }
    return map.get(key);
}