From a035067f8053be423e1819e027def05370e8b0a0 Mon Sep 17 00:00:00 2001 From: Hammy Date: Sun, 27 Dec 2020 14:40:27 +0000 Subject: [PATCH] Demonstrate Multithreading (With Issues) --- .../src/multi/threading/Adder.java | 40 +++++++++++++++++++ .../src/multi/threading/Main.java | 35 ++++++++++++++++ 2 files changed, 75 insertions(+) create mode 100644 Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/multi/threading/Adder.java create mode 100644 Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/multi/threading/Main.java diff --git a/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/multi/threading/Adder.java b/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/multi/threading/Adder.java new file mode 100644 index 0000000..77309e6 --- /dev/null +++ b/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/multi/threading/Adder.java @@ -0,0 +1,40 @@ +package multi.threading; + +import java.io.BufferedReader; +import java.io.BufferedWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; + +public class Adder implements Runnable { + private final String inFile; + private final String outFile; + + public Adder(String inFile, String outFile) { + this.inFile = inFile; + this.outFile = outFile; + } + + public void 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); + } + + try (BufferedWriter writer = Files.newBufferedWriter(Paths.get(outFile))) { + writer.write("Total: " + total); + } + } + + @Override + public void run() { + try { + doAdd(); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/multi/threading/Main.java b/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/multi/threading/Main.java new file mode 100644 index 0000000..b6de88f --- /dev/null +++ b/Pluralsite/src/intermediate/Java Fundamentals - The Core Platform/src/multi/threading/Main.java @@ -0,0 +1,35 @@ +package multi.threading; + +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 { + String[] inFiles = { + rootResources + "inputFile1.txt", + rootResources + "inputFile2.txt", + rootResources + "inputFile3.txt", + rootResources + "inputFile4.txt", + rootResources + "inputFile5.txt", + rootResources + "inputFile6.txt" + }; + String[] outFiles = { + rootResources + "outputFile1.txt", + rootResources + "outputFile2.txt", + rootResources + "outputFile3.txt", + rootResources + "outputFile4.txt", + rootResources + "outputFile5.txt", + rootResources + "outputFile6.txt" + }; + Thread[] threads = new Thread[inFiles.length]; + + for(int i=0; i < inFiles.length; i++) { + Adder adder = new Adder(inFiles[i], outFiles[i]); + Thread thread = new Thread(adder); + thread.start(); + } + + for (Thread thread : threads) { + thread.join(); + } + } +}