Example usage for java.util.concurrent Executors newCachedThreadPool

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

Introduction

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

Prototype

public static ExecutorService newCachedThreadPool() 

Source Link

Document

Creates a thread pool that creates new threads as needed, but will reuse previously constructed threads when they are available.

Usage

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());
    }//w ww.j av  a2  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 executor = Executors.newCachedThreadPool();
    Future<?> future = executor.submit(() -> {
        try {/*from w  w  w .j av  a 2 s . 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) {
    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: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:Main.java

public static void main(String[] args) {
    Map<String, String> map = new ConcurrentHashMap<String, String>();
    for (int i = 0; i < MAP_SIZE; i++) {
        map.put("key:" + i, UUID.randomUUID().toString());
    }/*from ww  w  .  ja  v a 2  s  .  c  o m*/
    ExecutorService executor = Executors.newCachedThreadPool();
    Accessor a1 = new Accessor(map);
    Accessor a2 = new Accessor(map);
    Mutator m = new Mutator(map);
    executor.execute(a1);
    executor.execute(m);
    executor.execute(a2);
}

From source file:HttpServerDemo.java

public static void main(String[] args) throws IOException {
    InetSocketAddress addr = new InetSocketAddress(8080);
    HttpServer server = HttpServer.create(addr, 0);

    server.createContext("/", new MyHandler());
    server.setExecutor(Executors.newCachedThreadPool());
    server.start();//from  w w w  . java2s  . c o m
    System.out.println("Server is listening on port 8080");
}

From source file:lohbihler.process.Test.java

public static void main(final String[] args) throws Exception {
    final ExecutorService executorService = Executors.newCachedThreadPool();

    final Process process = new ProcessBuilder("cmd").start();
    final InputReader input = new InputReader(process.getInputStream());
    final InputReader error = new InputReader(process.getErrorStream());

    executorService.execute(input);//from w ww.j  av a2  s .c  o m
    executorService.execute(error);
    final OutputStreamWriter out = new OutputStreamWriter(process.getOutputStream());

    Thread.sleep(1000);
    System.out.println("Input: " + input.getInput());
    System.out.println("Error: " + error.getInput());
    System.out.println("Alive: " + process.isAlive());
    System.out.println();

    out.append("PING 1.1.1.1 -n 1 -w 5000\r\n");
    out.flush();

    for (int i = 0; i < 7; i++) {
        Thread.sleep(1000);
        System.out.println("Input: " + input.getInput());
        System.out.println("Error: " + error.getInput());
        System.out.println("Alive: " + process.isAlive());
        System.out.println();
    }

    out.append("PING 1.1.1.1 -n 1 -w 2000\r\n");
    out.flush();

    for (int i = 0; i < 4; i++) {
        Thread.sleep(1000);
        System.out.println("Input: " + input.getInput());
        System.out.println("Error: " + error.getInput());
        System.out.println("Alive: " + process.isAlive());
        System.out.println();
    }

    process.destroy();

    executorService.shutdown();
}

From source file:ThreadPoolTest.java

public static void main(String[] args) throws Exception {
    Scanner in = new Scanner(System.in);
    System.out.print("Enter base directory (e.g. /usr/local/jdk5.0/src): ");
    String directory = in.nextLine();
    System.out.print("Enter keyword (e.g. volatile): ");
    String keyword = in.nextLine();

    ExecutorService pool = Executors.newCachedThreadPool();

    MatchCounter counter = new MatchCounter(new File(directory), keyword, pool);
    Future<Integer> result = pool.submit(counter);

    try {//from ww w. j  a v  a  2s .c om
        System.out.println(result.get() + " matching files.");
    } catch (ExecutionException e) {
        e.printStackTrace();
    } catch (InterruptedException e) {
    }
    pool.shutdown();

    int largestPoolSize = ((ThreadPoolExecutor) pool).getLargestPoolSize();
    System.out.println("largest pool size=" + largestPoolSize);
}

From source file:com.netcore.hsmart.dataconsumers.SmsDataConsumer.java

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

    ExecutorService executor = Executors.newCachedThreadPool();

    for (int i = 1; i <= COUNTERS; i++) {
        Runnable worker = new SmsDataConsumerRunnable(Integer.toString(i), Integer.toString(GID));
        executor.execute(worker);/*from   ww w .ja v a2s  . co  m*/
    }
}

From source file:com.netcore.hsmart.dataconsumers.DlrDataConsumer.java

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

    ExecutorService executor = Executors.newCachedThreadPool();

    for (int i = 1; i <= COUNTERS; i++) {
        Runnable worker = new DlrDataConsumerRunnable(Integer.toString(i), Integer.toString(GID));
        executor.execute(worker);/*from   w  ww.  j a  v a  2 s  .com*/
    }
}