Posts

Showing posts from September, 2023

ExecutorService with newFixedThreadPool example

The Executor Framework provides different types of executors. Some of them are .. SingleThreadExecutor FixedThreadPool(n) CachedThreadPool ScheduledExecutor Java ExecutorService is an interface in java. It allow in maintaining the thread pool and assign the tasks. Threads execute asynchronously. package com.threads.example; import java.util.ArrayList; import java.util.List; 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; /** this is just name of the class. we are using the Callable interface for creating the task */ public class CallableExample { public static void main(String[] args ) throws InterruptedException, ExecutionException { /** Getting the number of cpu core and creating the thread pool according to cpu count */ int c = Runtime. getRuntime ().availableProcessors(); ExecutorSe...