ThreadPoolExecutor and ScheduledExecutorService (Two concrete class of ExecutorService )

ExecutorService is an interface which have two implementation 
1-ThreadPoolExecutor 
2-ScheduledExecutorService 


ThreadPoolExecutor
ThreadPool contains inside the ThreadPoolExecutor can contains a varing amount of thread
the number of threads can be find by variable provided
1-corePoolSize
2-maximumPoolSize

if thread count is less than the corePoolSize the new thread will be created even if the thread present in the idle state


int  corePoolSize  =    5;
int  maxPoolSize   =   10;
long keepAliveTime = 5000;

ExecutorService threadPoolExecutor =
        new ThreadPoolExecutor(
                corePoolSize,
                maxPoolSize,
                keepAliveTime,
                TimeUnit.MILLISECONDS,
                new LinkedBlockingQueue<Runnable>()
                );


ScheduledExecutorService 
it is used to run or execute the task after the delay or to execute repeatedly after the fixed interval of times

ScheduledExecutorService scheduledExecutorService =
        Executors.newScheduledThreadPool(5);

ScheduledFuture scheduledFuture =
    scheduledExecutorService.schedule(new Callable() {
        public Object call() throws Exception {
            System.out.println("Executed!");
            return "Called!";
        }
    },
    5,
    TimeUnit.SECONDS);
ScheduledExecutorService  is also an interface the implementer of this interface is ScheduledThreadPoolExecutor.

Method for scheduledExecutor service usage 
  • schedule (Callable task, long delay, TimeUnit timeunit)
  • schedule (Runnable task, long delay, TimeUnit timeunit)
  • scheduleAtFixedRate (Runnable, long initialDelay, long period, TimeUnit timeunit)
  • scheduleWithFixedDelay (Runnable, long initialDelay, long period, TimeUnit timeunit)


Comments