Showing posts with label ExecutorService. Show all posts
Showing posts with label ExecutorService. Show all posts

Java ExecutorService

We have seen Java Timer Class in our earlier tutorial to. ExecutorService will be better option than using Timer since configured with any number of threads but Timer has only one execution thread.
Another important point to ExecutorService is, if any run-time exception occurred inside TimerTask then current task will be canceled and rest will be continued in ExecutorService. Where as in Timer kill the Thread and following scheduled tasks won’t run further.
Lets see simple example's of how to run a repeated task at a specified interval using ExecutorService.
Java ExecutorService


Single Threaded:


public class JavaExecutorService {

 public static void main(String[] args) throws InterruptedException {
  
  TimerTask repeatedTask = new TimerTask() {
         public void run() {
             System.out.println("OUTPUT : " + Thread.currentThread().getName()  + " : Random Number : "+new Random().nextInt(1000));
         }
     };
     ScheduledExecutorService executor = Executors.newSingleThreadScheduledExecutor();
     long delay  = 1000L;
     long period = 1000L;
     executor.scheduleAtFixedRate(repeatedTask, delay, period, TimeUnit.MILLISECONDS);
 }
}


OUTPUT:


OUTPUT : pool-1-thread-1 : Random Number : 608
OUTPUT : pool-1-thread-1 : Random Number : 62
OUTPUT : pool-1-thread-1 : Random Number : 161
OUTPUT : pool-1-thread-1 : Random Number : 141
OUTPUT : pool-1-thread-1 : Random Number : 954
OUTPUT : pool-1-thread-1 : Random Number : 565
OUTPUT : pool-1-thread-1 : Random Number : 839
.
.
.




By Setting Thread-pool Size:


public class JavaExecutorService {

 public static void main(String[] args) throws InterruptedException {
  
  TimerTask repeatedTask = new TimerTask() {
         public void run() {
             System.out.println("OUTPUT : " + Thread.currentThread().getName()  + " : Random Number : "+new Random().nextInt(1000));
         }
     };

     //Setting threadpool size to 5
     ScheduledExecutorService executor = Executors.newScheduledThreadPool(5);
     long delay  = 1000L;
     long period = 1000L;
     executor.scheduleAtFixedRate(repeatedTask, delay, period, TimeUnit.MILLISECONDS);
 }
}


OUTPUT:


OUTPUT : pool-1-thread-1 : Random Number : 562
OUTPUT : pool-1-thread-1 : Random Number : 207
OUTPUT : pool-1-thread-2 : Random Number : 912
OUTPUT : pool-1-thread-1 : Random Number : 165
OUTPUT : pool-1-thread-3 : Random Number : 676
OUTPUT : pool-1-thread-2 : Random Number : 956
OUTPUT : pool-1-thread-2 : Random Number : 987
OUTPUT : pool-1-thread-2 : Random Number : 866
OUTPUT : pool-1-thread-2 : Random Number : 331
OUTPUT : pool-1-thread-3 : Random Number : 183
OUTPUT : pool-1-thread-3 : Random Number : 669
OUTPUT : pool-1-thread-3 : Random Number : 617
OUTPUT : pool-1-thread-3 : Random Number : 719
OUTPUT : pool-1-thread-3 : Random Number : 757
OUTPUT : pool-1-thread-3 : Random Number : 728
OUTPUT : pool-1-thread-3 : Random Number : 77
OUTPUT : pool-1-thread-5 : Random Number : 66
OUTPUT : pool-1-thread-2 : Random Number : 64
OUTPUT : pool-1-thread-4 : Random Number : 643
.
.
.