Example of ScheduledThreadPoolExecutor in Java

By Arvind Rai, April 21, 2019
ScheduledThreadPoolExecutor belongs to java.util.concurrent package. While creating object of ScheduledThreadPoolExecutor, we need to pass core pool Size. ScheduledThreadPoolExecutor executes and schedules task. We create its object as following.
 ScheduledThreadPoolExecutor stpe = new ScheduledThreadPoolExecutor(corePoolSize); 
where corePoolSize is the pool size i.e number of threads that can be executed by ScheduledThreadPoolExecutor at a time.
1. execute method of ScheduledThreadPoolExecutor will execute only Runnable task.
 stpe.execute(new ScheduledThreadPoolExecutorTest().new RunnableThread()); 
2. schedule method of ScheduledThreadPoolExecutor can accept Callable and Runnable task both. It returns ScheduledFuture instance.
ScheduledFuture<Integer> sf = 
  stpe.schedule(new ScheduledThreadPoolExecutorTest().new CallableThread(), 2, TimeUnit.SECONDS); 
Find the complete example.
package com.concretepage;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
public class ScheduledThreadPoolExecutorTest {
    public static void main(String... args) throws InterruptedException, ExecutionException {
        int corePoolSize=2;
        //creates ScheduledThreadPoolExecutor object with number of thread 2
        ScheduledThreadPoolExecutor stpe = new ScheduledThreadPoolExecutor(corePoolSize);
        //starts runnable thread 
        stpe.execute(new ScheduledThreadPoolExecutorTest().new RunnableThread());
        //starts callable thread that will start after 2 seconds
        ScheduledFuture<Integer> sf = stpe.schedule(new ScheduledThreadPoolExecutorTest().new CallableThread(), 2,
                TimeUnit.SECONDS);
        //gets value returned by callable thread
        int res=sf.get();
        System.out.println("value returned by Callable Thread."+res);
         //returns active thread 
        int activeCnt=stpe.getActiveCount();
        System.out.println("activeCnt:"+activeCnt);
        //stops all the threads in ScheduledThreadPoolExecutor
        stpe.shutdownNow();
        System.out.println(stpe.isShutdown());
    }
    //runnable thread
    class RunnableThread implements Runnable {
        @Override
        public void run() {
            int cnt = 0;
            for (; cnt < 5; cnt++) {
                System.out.println("run:" + cnt);
            }
        }
    }
    // callable thread that will return value 
    class CallableThread implements Callable<Integer> {
        @Override
        public Integer call() throws Exception {
            int cnt = 0;
            for (; cnt < 5; cnt++) {
                System.out.println("call:" + cnt);
            }
            return cnt;
        }
    }
}
Output
run:0
run:1
run:2
run:3
run:4
call:0
call:1
call:2
call:3
call:4
value returned by Callable Thread.5
activeCnt:0
true 
POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us