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 void main(String[] args) {
    int NTHREADS = 25;
    ExecutorService exec = Executors.newFixedThreadPool(NTHREADS);
    exec.submit(new MailConsumer());
    exec.submit(new MailProducer());
    System.out.println("inside main");
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    ExecutorService executor = Executors.newCachedThreadPool();
    Future<?> future = executor.submit(() -> {
        try {//  www.j  ava2s  .co m
            Thread.sleep(1000);
        } catch (Exception e) {
            System.out.println("Epic fail.");
        }
    });
    System.out.println("Waiting for task to finish..");
    future.get();
    System.out.println("Task finished!");
    executor.shutdown();
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    ExecutorService es = Executors.newFixedThreadPool(3);
    Future<Double> f = es.submit(new Avg());
    Future<Integer> f2 = es.submit(new Factorial());

    System.out.println(f.get());/* w w w  . j  av a  2s. com*/
    System.out.println(f2.get());
    es.shutdown();
}

From source file:Main.java

public static void main(String args[]) throws Exception {
    ExecutorService es = Executors.newFixedThreadPool(3);
    Future<Double> f = es.submit(new Task1());
    Future<Integer> f2 = es.submit(new Task2());

    System.out.println(f.get());//  ww  w. ja  v a  2 s .  c o m
    System.out.println(f2.get());
    es.shutdown();
}

From source file:Main.java

public static void main(String[] args) {
    CyclicBarrier barrier = new CyclicBarrier(2);
    Receiver receiver = new Receiver(barrier);
    Sender sender = new Sender(barrier);
    ExecutorService executor = Executors.newFixedThreadPool(2);
    executor.submit(receiver);
    executor.submit(sender);// w ww .j a  v a 2 s .  c  o  m
}

From source file:Main.java

public static void main(String[] args) {
    Callable<Object> badTask = () -> {
        throw new RuntimeException("Throwing exception from task execution...");
    };/*w  ww  .j ava  2s. co m*/
    ExecutorService exec = Executors.newSingleThreadExecutor();
    Future submittedTask = exec.submit(badTask);
    try {
        Object result = submittedTask.get();
    } catch (ExecutionException e) {
        System.out.println(e.getMessage());
        System.out.println(e.getCause().getMessage());
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    exec.shutdown();
}

From source file:Main.java

public static void main(String[] args) throws InterruptedException {
    Main task = new Main();
    int threads = Runtime.getRuntime().availableProcessors();
    ExecutorService pool = Executors.newFixedThreadPool(threads);
    for (int i = 0; i < threads; i++) {
        pool.submit(task);
    }//from   ww w.  ja v  a 2 s  .c om
    pool.awaitTermination(120, TimeUnit.SECONDS);
}

From source file:com.level3.hiper.dyconn.be.Main.java

public static void main(String... args) {

    try {//  ww  w  .j ava  2s .c  om
        String bootstrap = "/dyconn-be-toml.cfg";
        CommandLineParser parser = new DefaultParser();
        Options options = new Options();
        options.addOption("c", "config-file", true, "configuration for hapi dyconn module");
        try {
            CommandLine line = parser.parse(options, args);
            if (line.hasOption("config-file")) {
                bootstrap = line.getOptionValue("config-file");
            }
        } catch (ParseException ex) {
            log.error("command line", ex);
            return;
        }

        // read config file
        log.info("loading configuration");
        Config.instance().initialize(bootstrap);

        // initialize queue subsystem
        log.info("initializing messaging");
        Broker.instance().initialize();

        // initilaize persistence
        log.info("starting exector");
        ExecutorService executor = Executors.newSingleThreadExecutor();
        executor.submit(new MsgReceiver());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:Main.java

public static void main(String[] args) throws Exception {

    ExecutorService ex = Executors.newSingleThreadExecutor();

    Future<Integer> future =
            // This Lambda evaluated to Callable<Integer>
            ex.submit(() -> ThreadLocalRandom.current().nextInt(1, 10));

    System.out.println("Randomized value: " + future.get());

}

From source file:Main.java

public static void main(String[] args) throws InterruptedException, ExecutionException {

    ExecutorService ex = Executors.newSingleThreadExecutor();

    Future<Integer> future =
            // This Lambda evaluated to Callable<Integer>
            ex.submit(() -> ThreadLocalRandom.current().nextInt(1, 10));

    System.out.println("Randomized value: " + future.get());

}