Last active
October 30, 2019 08:42
-
-
Save prameshbhattarai/f2fd67b0eeeadef8d0dc2ab99c4b8817 to your computer and use it in GitHub Desktop.
Executor Service: Submit method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class SubmitTask { | |
private static final int DEFAULT_THREAD_COUNT = 4; // default number of threads | |
private final ThreadPoolExecutor executorService; | |
public SubmitTask() { | |
this.executorService = (ThreadPoolExecutor) Executors.newFixedThreadPool(DEFAULT_THREAD_COUNT); | |
} | |
// if we only submit task to executor service then our thread pool will not closed | |
// even after completion of processing callable threads. | |
public <T> Future<T> submitTask(Callable<T> callable) { | |
return executorService.submit(callable); | |
} | |
public void invokeAll(List<Callable<Long>> callableList) { | |
try { | |
// execute all callable and store the results to future | |
List<Future<Long>> futures = executorService.invokeAll(callableList); | |
for(Future<Long> future : futures) { | |
System.out.println("Is Done " + future.isDone()); | |
Long processedData = future.get(); | |
System.out.println("Thread Id " + processedData); | |
} | |
System.out.println("Force stop executing task"); | |
executorService.shutdownNow(); | |
} catch (InterruptedException | ExecutionException e) { | |
e.printStackTrace(); | |
} | |
} | |
public static void main(String... args) { | |
List<Callable<Long>> callableList = new ArrayList<>(); | |
Callable<Long> callable = () -> { | |
final long startTime = System.nanoTime(); | |
System.out.println("Start to Submit: Thread Name " + Thread.currentThread().getId() + " name " + Thread.currentThread().getName()); | |
try { | |
Thread.sleep(randomMilliSecond()); | |
final long endTime = System.nanoTime(); | |
System.out.println("Complete processing, Thread Id: " + Thread.currentThread().getId() + ", Thread Name: " + Thread.currentThread().getName()); | |
System.out.println("Time to process, Thread Id: " + Thread.currentThread().getId() + ", Thread Name: " + Thread.currentThread().getName() + ", time : " + (endTime - startTime)); | |
} catch (InterruptedException e) { | |
e.printStackTrace(); | |
} | |
return Thread.currentThread().getId(); | |
}; | |
callableList.add(callable); | |
callableList.add(callable); | |
callableList.add(callable); | |
callableList.add(callable); | |
callableList.add(callable); | |
SubmitTask submit = new SubmitTask(); | |
List<Future<Long>> futures = callableList.stream().map(submit::submitTask).collect(Collectors.toList()); | |
// for(Future<Long> future : futures) { | |
// System.out.println("Is Done: " + future.isDone()); | |
// } | |
submit.invokeAll(callableList); | |
} | |
private static long randomMilliSecond(){ | |
return (long)(Math.random() * 10) * 1000; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment