From 972c5de9037774cb0420147ad3a96013c4f662cf Mon Sep 17 00:00:00 2001 From: Hammy Date: Sun, 27 Dec 2020 15:10:08 +0000 Subject: [PATCH] Use thread pools (ExecutorService) --- .../threading/using/thread/pools/Adder.java | 32 +++++++++++++++++ .../threading/using/thread/pools/Main.java | 34 +++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/multi/threading/using/thread/pools/Adder.java create mode 100644 Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/multi/threading/using/thread/pools/Main.java diff --git a/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/multi/threading/using/thread/pools/Adder.java b/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/multi/threading/using/thread/pools/Adder.java new file mode 100644 index 0000000..0be0de9 --- /dev/null +++ b/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/multi/threading/using/thread/pools/Adder.java @@ -0,0 +1,32 @@ +package multi.threading.using.thread.pools; + +import java.io.BufferedReader; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.concurrent.Callable; + +public class Adder implements Callable { + private final String inFile; + + public Adder(String inFile) { + this.inFile = inFile; + } + + public int doAdd() throws IOException { + int total = 0; + String line; + + try (BufferedReader reader = Files.newBufferedReader(Paths.get(inFile))) { + while ((line = reader.readLine()) != null) + total += Integer.parseInt(line); + } + + return total; + } + + @Override + public Integer call() throws IOException { + return doAdd(); + } +} diff --git a/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/multi/threading/using/thread/pools/Main.java b/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/multi/threading/using/thread/pools/Main.java new file mode 100644 index 0000000..78e0777 --- /dev/null +++ b/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/multi/threading/using/thread/pools/Main.java @@ -0,0 +1,34 @@ +package multi.threading.using.thread.pools; + +import java.util.concurrent.*; + +public class Main { + private static final String rootResources = "C:\\Users\\sgoud\\JavaProjects\\Learning-Java\\Pluralsite\\src\\intermediate\\Java Fundamentals - The Core Platform\\resources\\threading\\"; + + public static void main(String[] args) throws InterruptedException, ExecutionException { + String[] inFiles = { + rootResources + "inputFile1.txt", + rootResources + "inputFile2.txt", + rootResources + "inputFile3.txt", + rootResources + "inputFile4.txt", + rootResources + "inputFile5.txt", + rootResources + "inputFile6.txt" + }; + ExecutorService executorService = Executors.newFixedThreadPool(3); + Future[] results = new Future[inFiles.length]; + + for(int i=0; i < inFiles.length; i++) { + Adder adder = new Adder(inFiles[i]); + results[i] = executorService.submit(adder); + } + + for (Future result : results) { + System.out.println(result.get()); + } + + executorService.shutdown(); + if (executorService.awaitTermination(60, TimeUnit.SECONDS)) { + System.out.println("Operations Executed Successfully!"); + } + } +}