Home  >  Core Java

Example of newCachedThreadPool in Java

By Arvind Rai, February 27, 2013
newCachedThreadPool() is the method of java.util.concurrent.Executors class. newCachedThreadPool() creates the pool of threads. Whenever a thread is needed, pool returns a thread from cache and if not available, a new thread is created for a short time. When the timeout of thread is over, that thread is vanished. Threads are very short time lived created by newCachedThreadPool(). By default the time is 60 second. We can assign another time by constructor. There is no fixed pool size. This type of thread pool is useful when threads are idle for a long time. newCachedThreadPool() will not allow to remain a thread idle for a long time and so thread will not use any resource. Hence performance is increased.
 ExecutorService exService = Executors.newCachedThreadPool();
 exService.execute(new NewFixedThreadPoolTest().new RunnableThread());
 Future<Integer> future=exService.submit(new NewFixedThreadPoolTest().new CallableThread()); 
In the above code snippet, a cached thread pool with unlimited number of threads has been created but constraint is memory. Another newCachedThreadPool(ThreadFactory) is available in which we can assign our user defined thread pool. execute() method runs only Runnable thread and submit() method runs Callable as well as Runnable thread. With help of Future, we get the value returned by Callable thread. Find the complete example of newCachedThreadPool().
NewCachedThreadPoolTest.java
package com.concretepage;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
public class NewCachedThreadPoolTest {
    public static void main(String... args) throws InterruptedException, ExecutionException{
        //creates cached thread pool
        ExecutorService exService = Executors.newCachedThreadPool();
        // runnable thread start to execute.
        exService.execute(new NewCachedThreadPoolTest().new RunnableThread());
        //callable thread starts to execute
        Future<Integer> future=exService.submit(new NewCachedThreadPoolTest().new CallableThread());
        //gets value of callable thread
        int val=future.get();
        System.out.println(val);
        //checks for thread termination
        boolean isTerminated=exService.isTerminated();
        System.out.println(isTerminated);
        // waits for termination for 30 seconds only
        exService.awaitTermination(30,TimeUnit.SECONDS);
        exService.shutdownNow();
    }
    //Callable thread
    class CallableThread implements Callable<Integer> {
        @Override
        public Integer call() {
            int cnt = 0;
            for (; cnt < 5; cnt++) {
                System.out.println("call:" + cnt);
            }
            return cnt;
        }
    }
    //Runnable thread
    class RunnableThread implements Runnable {
        @Override
        public void run() {
            int cnt = 0;
            for (; cnt < 5; cnt++) {
                System.out.println("run:" + cnt);
            }
        }
    }
 }
Output
run:0
run:1
run:2
run:3
run:4
call:0
call:1
call:2
call:3
call:4
5
false
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us