Example usage for java.util.concurrent ExecutorService submit

List of usage examples for java.util.concurrent ExecutorService submit

Introduction

In this page you can find the example usage for java.util.concurrent ExecutorService submit.

Prototype

Future<?> submit(Runnable task);

Source Link

Document

Submits a Runnable task for execution and returns a Future representing that task.

Usage

From source file:Main.java

public static <T> T executeWithTimeout(Callable<T> task, int milliseconds) {
    ExecutorService executor = Executors.newCachedThreadPool();
    Future<T> future = executor.submit(task);
    T result;/*from   w w  w . ja v a  2  s  .  c o m*/
    try {
        result = future.get(milliseconds, TimeUnit.MILLISECONDS);
    } catch (Exception e) { //handle timeout and other exceptions
        e.printStackTrace();
        result = null;
    } finally {
        future.cancel(true);
    }
    return result;
}

From source file:Main.java

public static void runAllServices(Runnable... runnables) {
    ExecutorService service = Executors.newCachedThreadPool();
    for (Runnable runnable : runnables) {
        service.submit(runnable);
    }/*from w  w  w.j av  a 2 s  . c  o  m*/
    service.shutdown();
}

From source file:Main.java

public static <T> T getFutureResult(Callable<T> task, long timeout) throws TimeoutException {
    try {//  w  w  w  .  ja  va2s. c  o 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

public static String getDomainAddress(final String domain) {
    try {/*from ww  w  .  java2 s  . c  o  m*/
        ExecutorService exec = Executors.newCachedThreadPool();
        Future<String> fs = exec.submit(new Callable<String>() {
            @Override
            public String call() throws Exception {
                InetAddress inetAddress;
                try {
                    inetAddress = InetAddress.getByName(domain);
                    return inetAddress.getHostAddress();
                } catch (UnknownHostException e) {
                    e.printStackTrace();
                }
                return null;
            }
        });
        return fs.get();
    } catch (InterruptedException | ExecutionException e) {
        e.printStackTrace();
    }
    return null;
}

From source file:Main.java

/**
 * Runs and blocking waits for the given callable to finish for the given
 * time. Returns <code>null</code> if timeouts waiting for callable value.
 * //  w w  w.  ja v a2 s . c  o m
 * @param millisTimeout
 * @param callable
 * @return
 */
public static <R> R runWithTimeout(long millisTimeout, Callable<R> callable) {
    ExecutorService singleThreadExecutor = Executors.newFixedThreadPool(1);
    Future<R> future = singleThreadExecutor.submit(callable);
    try {
        return future.get(millisTimeout, TimeUnit.MILLISECONDS);
    } catch (InterruptedException | ExecutionException | TimeoutException e) {
    } finally {
        singleThreadExecutor.shutdown();
    }
    return null;
}

From source file:Main.java

/**
 * <p>inokeInOtherThread</p>
 *
 * @param callable a Callable object.//from   ww w.j av  a  2 s.  co  m
 * @return a T object.
 *
 * @throws ExecutionException   if any.
 * @throws InterruptedException if any.
 */
@Nullable
public static <T> T inokeInOtherThread(@Nonnull Callable<T> callable)
        throws ExecutionException, InterruptedException {
    ExecutorService executor = Executors.newSingleThreadExecutor();
    try {
        Future<T> future = executor.submit(callable);
        return future.get();
    } finally {
        executor.shutdown();
    }
}

From source file:Main.java

public static <T> List<Future<T>> call(int thred, Callable<T> callable) {
    ExecutorService es = Executors.newCachedThreadPool();
    List<Future<T>> list = Lists.newArrayList();
    for (int i = 0; i < thred; i++) {
        list.add(es.submit(callable));
    }//from w ww .j av a2  s.c om
    return list;
}

From source file:Main.java

public static <T> List<Future<T>> call(Collection<Callable<T>> callables) {
    ExecutorService es = Executors.newCachedThreadPool();
    List<Future<T>> list = Lists.newArrayList();
    for (Callable<T> callable : callables) {
        list.add(es.submit(callable));
    }//from w  ww .  java 2s.  c o m
    return list;
}

From source file:Main.java

static <T> List<T> submit(List<? extends Callable<T>> taskList, final String taskName) throws Exception {

    List<Future<T>> futureList = new ArrayList();

    List<T> result = new ArrayList<>();

    ExecutorService pool = newFixedThreadPool(taskList.size(), taskName);

    for (Callable<T> callable : taskList) {
        futureList.add(pool.submit(callable));
    }/*from   w  w  w.j a  v a 2 s . c  om*/

    pool.shutdown();

    for (Future<T> f : futureList) {
        try {
            result.add(f.get());
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    return result;
}

From source file:baggage.BaseTestCase.java

protected static void attempt(Runnable task) {
    ExecutorService executorService = Executors.newFixedThreadPool(1);
    executorService.submit(task);
    executorService.shutdown();//from   w  ww .j  a  v a2s  . com
    try {
        Thread.sleep(100);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}