Example usage for java.util.concurrent ExecutorService shutdown

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

Introduction

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

Prototype

void shutdown();

Source Link

Document

Initiates an orderly shutdown in which previously submitted tasks are executed, but no new tasks will be accepted.

Usage

From source file:Main.java

public static void main(String[] args) {
    Runnable badTask = () -> {
        throw new RuntimeException("Throwing exception  from  task execution...");
    };//from   w w w  . ja  v a 2s . c  om

    ExecutorService exec = Executors.newSingleThreadExecutor();
    exec.execute(badTask);
    exec.shutdown();
}

From source file:Main.java

public static void main(String[] args) {
    ExecutorService pool = Executors.newCachedThreadPool();
    Main app = new Main();
    for (int i = 0; i < 1000; i++) {
        pool.execute(app.new Adding());
    }//from   w  ww  .j a  v a  2 s. c o  m
    pool.shutdown();
    while (!pool.isTerminated()) {
        System.out.println(" Is it done? : " + pool.isTerminated());
    }
    System.out.println(" Is it done? : " + pool.isTerminated());
    System.out.println("Sum is " + app.sum);
}

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());/* w  ww .j a  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[]) 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 ww .  j  a  va  2 s.  c om*/
    System.out.println(f2.get());
    es.shutdown();
}

From source file:Main.java

public static void main(String[] args) {
    PrintTask task1 = new PrintTask("thread1");
    PrintTask task2 = new PrintTask("thread2");
    PrintTask task3 = new PrintTask("thread3");

    System.out.println("Starting threads");

    ExecutorService threadExecutor = Executors.newCachedThreadPool();

    threadExecutor.execute(task1); // start task1
    threadExecutor.execute(task2); // start task2
    threadExecutor.execute(task3); // start task3

    threadExecutor.shutdown(); // shutdown worker threads

    System.out.println("Threads started, main ends\n");
}

From source file:RunnableTask.java

public static void main(String[] args) {
    final int THREAD_COUNT = 3;
    final int LOOP_COUNT = 3;
    final int TASK_COUNT = 5;

    // Get an executor with three threads in its thread pool
    ExecutorService exec = Executors.newFixedThreadPool(THREAD_COUNT);

    // Create five tasks and submit them to the executor
    for (int i = 1; i <= TASK_COUNT; i++) {
        RunnableTask task = new RunnableTask(i, LOOP_COUNT);
        exec.submit(task);/*w ww. j a  va  2  s.co m*/
    }
    exec.shutdown();
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    System.out.println("Thread pool size = " + MAX_THREADS);
    ExecutorService executor = Executors.newFixedThreadPool(MAX_THREADS);
    Future previousFuture = null;
    for (int i = 0; i < MAX_THREADS; i++) {
        JobRunnable job = new JobRunnable(i, previousFuture);
        previousFuture = executor.submit(job);
    }// w ww  .  j  a va 2 s . c  om
    executor.shutdown();
    executor.awaitTermination(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
    System.out.println("Program done.");
}

From source file:MainClass.java

public static void main(String[] args) {
    PrintTask task1 = new PrintTask("thread1");
    PrintTask task2 = new PrintTask("thread2");
    PrintTask task3 = new PrintTask("thread3");

    System.out.println("Starting threads");

    ExecutorService threadExecutor = Executors.newCachedThreadPool();

    // start threads and place in runnable state
    threadExecutor.execute(task1); // start task1
    threadExecutor.execute(task2); // start task2
    threadExecutor.execute(task3); // start task3

    threadExecutor.shutdown(); // shutdown worker threads

    System.out.println("Threads started, main ends\n");
}

From source file:WordLengthCallable.java

public static void main(String[] args) throws Exception {
    int THREAD_COUNT = 4;
    ExecutorService execService = Executors.newFixedThreadPool(THREAD_COUNT);
    CompletionService<Integer> completionService = new ExecutorCompletionService<>(execService);

    for (int i = 0; i < THREAD_COUNT; i++) {
        completionService.submit(new WordLengthCallable());
    }/*from   www .j a  va2 s  . c om*/
    execService.shutdown();
    while (!execService.isTerminated()) {
        int result = completionService.take().get().intValue();
        System.out.println("Result is: " + result);
    }
    Thread.sleep(1000);
    System.out.println("done!");
}

From source file:Main.java

public static void main(final String[] args) throws Exception {
    Random RND = new Random();
    ExecutorService es = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());
    List<Future<String>> results = new ArrayList<>(10);
    for (int i = 0; i < 10; i++) {
        results.add(es.submit(new TimeSliceTask(RND.nextInt(10), TimeUnit.SECONDS)));
    }/*  w  ww  .  j a v  a  2s .  co m*/
    es.shutdown();
    while (!results.isEmpty()) {
        Iterator<Future<String>> i = results.iterator();
        while (i.hasNext()) {
            Future<String> f = i.next();
            if (f.isDone()) {
                System.out.println(f.get());
                i.remove();
            }
        }
    }
}