From 90874bf50db1c19d5794fa3b5f66303a383eccb1 Mon Sep 17 00:00:00 2001 From: zaker-666 <1075151259china@gmail.com> Date: Sat, 11 Jan 2020 16:49:26 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E6=8F=90=E4=BA=A4=E4=BA=86=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../github/hcsp/multithread/FileDetector.java | 52 +++++++++++++++++++ .../multithread/MultiThreadWordCount1.java | 46 +++++++++++++++- .../multithread/MultiThreadWordCount2.java | 30 +++++++++++ .../multithread/MultiThreadWordCount3.java | 31 +++++++++++ .../multithread/MultiThreadWordCount4.java | 30 +++++++++++ .../multithread/MultiThreadWordCount5.java | 51 ++++++++++++++++++ 6 files changed, 238 insertions(+), 2 deletions(-) create mode 100644 src/main/java/com/github/hcsp/multithread/FileDetector.java diff --git a/src/main/java/com/github/hcsp/multithread/FileDetector.java b/src/main/java/com/github/hcsp/multithread/FileDetector.java new file mode 100644 index 0000000..6dfc027 --- /dev/null +++ b/src/main/java/com/github/hcsp/multithread/FileDetector.java @@ -0,0 +1,52 @@ +package com.github.hcsp.multithread; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CountDownLatch; + +public class FileDetector extends Thread { + List> result; + Object object; + File file; + CountDownLatch countDownLatch; + + public FileDetector(List> countResults, Object object, File file) { + result = countResults; + this.object = object; + this.file = file; + } + + public FileDetector(List> result, Object object, File file, CountDownLatch countDownLatch) { + this.result = result; + this.object = object; + this.file = file; + this.countDownLatch = countDownLatch; + } + + @Override + public void run() { + System.out.println("子线程:" + Thread.currentThread().getName() + "执行"); + Map myCountResult = new HashMap<>(); + try (BufferedReader bufferedReader = new BufferedReader(new FileReader(file));) { + String line = ""; + while ((line = bufferedReader.readLine()) != null) { + String[] split = line.split(" "); + for (String word : split) { + if (myCountResult.containsKey(word)) { + myCountResult.put(word, myCountResult.get(word) + 1); + } else { + myCountResult.put(word, 1); + } + } + } + result.add(myCountResult); + } catch (IOException e) { + e.printStackTrace(); + } + } +} diff --git a/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount1.java b/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount1.java index 15c4aae..6af03da 100644 --- a/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount1.java +++ b/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount1.java @@ -1,12 +1,54 @@ package com.github.hcsp.multithread; +import java.io.BufferedReader; import java.io.File; +import java.io.FileReader; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import java.util.Map; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; public class MultiThreadWordCount1 { // 使用threadNum个线程,并发统计文件中各单词的数量 - public static Map count(int threadNum, List files) { - return null; + public static Map count(int threadNum, List files) throws ExecutionException, InterruptedException { + ExecutorService executorService = Executors.newFixedThreadPool(threadNum); + Map countResult = new HashMap<>(); + List>> futures = new ArrayList<>(); + for (File file : files) { + futures.add(executorService.submit(() -> { + Map result = new HashMap<>(); + BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); + String line = ""; + while ((line = bufferedReader.readLine()) != null) { + String[] split = line.split(" "); + for (String word : split) { + if (result.containsKey(word)) { + result.put(word, result.get(word) + 1); + } else { + result.put(word, 1); + } + } + } + return result; + })); + } + Map waitforMerge; + for (Future> future : futures) { + waitforMerge = future.get(); + mergeResulttoFinal(waitforMerge, countResult); + } + return countResult; + } + + private static void mergeResulttoFinal(Map waitforMerge, Map countResult) { + for (Map.Entry entry : waitforMerge.entrySet()) { + String word = entry.getKey(); + int i = countResult.getOrDefault(word, 0) + entry.getValue(); + countResult.put(word, i); + } } } diff --git a/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount2.java b/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount2.java index 3f23afa..8b7616b 100644 --- a/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount2.java +++ b/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount2.java @@ -1,8 +1,38 @@ package com.github.hcsp.multithread; +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + public class MultiThreadWordCount2 { // 使用threadNum个线程,并发统计文件中各单词的数量 // public static Map count(int threadNum, List files) { // return null; // } + public static Map count(int threadNum, List files) throws InterruptedException { + List> results = new ArrayList<>(); + Map countResult = new HashMap<>(); + buildDetector(threadNum, files, results); + Thread.sleep(100); + for (Map waitforMerge : results) { + mergeResulttoFinal(waitforMerge, countResult); + } + return countResult; + } + + private static void buildDetector(int threadNum, List files, List> results) { + for (int i = 0; i < threadNum; i++) { + new FileDetector(results, null, files.get(i)).start(); + } + } + + private static void mergeResulttoFinal(Map waitforMerge, Map countResult) { + for (Map.Entry entry : waitforMerge.entrySet()) { + String word = entry.getKey(); + int i = countResult.getOrDefault(word, 0) + entry.getValue(); + countResult.put(word, i); + } + } } diff --git a/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount3.java b/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount3.java index e180ee2..4eedd97 100644 --- a/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount3.java +++ b/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount3.java @@ -1,8 +1,39 @@ package com.github.hcsp.multithread; +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.CountDownLatch; + public class MultiThreadWordCount3 { // 使用threadNum个线程,并发统计文件中各单词的数量 // public static Map count(int threadNum, List files) { // return null; // } + public static Map count(int threadNum, List files) throws InterruptedException { + final CountDownLatch countDownLatch = new CountDownLatch(threadNum); + List> results = new ArrayList<>(); + Map countResult = new HashMap<>(); + buildDetector(threadNum, files, results, countDownLatch); + for (Map waitforMerge : results) { + mergeResulttoFinal(waitforMerge, countResult); + } + return countResult; + } + + private static void buildDetector(int threadNum, List files, List> results, CountDownLatch countDownLatch) { + for (int i = 0; i < threadNum; i++) { + new FileDetector(results, null, files.get(i), countDownLatch).start(); + } + } + + private static void mergeResulttoFinal(Map waitforMerge, Map countResult) { + for (Map.Entry entry : waitforMerge.entrySet()) { + String word = entry.getKey(); + int i = countResult.getOrDefault(word, 0) + entry.getValue(); + countResult.put(word, i); + } + } } diff --git a/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount4.java b/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount4.java index 13e0cad..c028a6b 100644 --- a/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount4.java +++ b/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount4.java @@ -1,8 +1,38 @@ package com.github.hcsp.multithread; +import java.io.File; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + public class MultiThreadWordCount4 { // 使用threadNum个线程,并发统计文件中各单词的数量 // public static Map count(int threadNum, List files) { // return null; // } + public static Map count(int threadNum, List files) throws InterruptedException { + List> results = new ArrayList<>(); + Map countResult = new HashMap<>(); + buildDetector(threadNum, files, results); + Thread.sleep(100); + for (Map waitforMerge : results) { + mergeResulttoFinal(waitforMerge, countResult); + } + return countResult; + } + + private static void buildDetector(int threadNum, List files, List> results) { + for (int i = 0; i < threadNum; i++) { + new FileDetector(results, null, files.get(i)).start(); + } + } + + private static void mergeResulttoFinal(Map waitforMerge, Map countResult) { + for (Map.Entry entry : waitforMerge.entrySet()) { + String word = entry.getKey(); + int i = countResult.getOrDefault(word, 0) + entry.getValue(); + countResult.put(word, i); + } + } } diff --git a/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount5.java b/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount5.java index 0c398cf..b82c16a 100644 --- a/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount5.java +++ b/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount5.java @@ -1,8 +1,59 @@ package com.github.hcsp.multithread; +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.IOException; +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.concurrent.ExecutionException; + public class MultiThreadWordCount5 { // 使用threadNum个线程,并发统计文件中各单词的数量 // public static Map count(int threadNum, List files) { // return null; // } + private static List> results = new ArrayList<>(); + + public static Map count(int threadNum, List files) throws ExecutionException, InterruptedException { + Map countResult = new HashMap<>(); + files.parallelStream().forEach(file -> { + try { + countResult(file); + } catch (IOException e) { + e.printStackTrace(); + } + }); + for (Map result : results) { + mergeResulttoFinal(result, countResult); + } + return countResult; + } + + private static void countResult(File file) throws IOException { + Map result = new HashMap<>(); + BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); + String line = ""; + while ((line = bufferedReader.readLine()) != null) { + String[] split = line.split(" "); + for (String word : split) { + if (result.containsKey(word)) { + result.put(word, result.get(word) + 1); + } else { + result.put(word, 1); + } + } + } + results.add(result); + } + + private static void mergeResulttoFinal(Map waitforMerge, Map countResult) { + for (Map.Entry entry : waitforMerge.entrySet()) { + String word = entry.getKey(); + int i = countResult.getOrDefault(word, 0) + entry.getValue(); + countResult.put(word, i); + } + } } From a075833e2b0b33364b43ec2c86b64ecc9bcc929e Mon Sep 17 00:00:00 2001 From: zaker-666 <1075151259china@gmail.com> Date: Sat, 11 Jan 2020 17:04:38 +0800 Subject: [PATCH 2/4] fix --- .../multithread/MultiThreadWordCount5.java | 38 +++++-------------- 1 file changed, 9 insertions(+), 29 deletions(-) diff --git a/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount5.java b/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount5.java index b82c16a..9b468b1 100644 --- a/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount5.java +++ b/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount5.java @@ -1,9 +1,6 @@ package com.github.hcsp.multithread; -import java.io.BufferedReader; import java.io.File; -import java.io.FileReader; -import java.io.IOException; import java.util.ArrayList; import java.util.HashMap; import java.util.List; @@ -15,38 +12,21 @@ public class MultiThreadWordCount5 { // public static Map count(int threadNum, List files) { // return null; // } - private static List> results = new ArrayList<>(); - - public static Map count(int threadNum, List files) throws ExecutionException, InterruptedException { + public static Map count(int threadNum, List files) throws InterruptedException { + List> results = new ArrayList<>(); Map countResult = new HashMap<>(); - files.parallelStream().forEach(file -> { - try { - countResult(file); - } catch (IOException e) { - e.printStackTrace(); - } - }); - for (Map result : results) { - mergeResulttoFinal(result, countResult); + buildDetector(threadNum, files, results); + Thread.sleep(100); + for (Map waitforMerge : results) { + mergeResulttoFinal(waitforMerge, countResult); } return countResult; } - private static void countResult(File file) throws IOException { - Map result = new HashMap<>(); - BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); - String line = ""; - while ((line = bufferedReader.readLine()) != null) { - String[] split = line.split(" "); - for (String word : split) { - if (result.containsKey(word)) { - result.put(word, result.get(word) + 1); - } else { - result.put(word, 1); - } - } + private static void buildDetector(int threadNum, List files, List> results) { + for (int i = 0; i < threadNum; i++) { + new FileDetector(results, null, files.get(i)).start(); } - results.add(result); } private static void mergeResulttoFinal(Map waitforMerge, Map countResult) { From 53873335cca27415199c169f9b6ed7992af7ad25 Mon Sep 17 00:00:00 2001 From: zaker-666 <1075151259china@gmail.com> Date: Sat, 11 Jan 2020 17:07:29 +0800 Subject: [PATCH 3/4] fix --- .../java/com/github/hcsp/multithread/MultiThreadWordCount5.java | 1 - 1 file changed, 1 deletion(-) diff --git a/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount5.java b/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount5.java index 9b468b1..8db27ad 100644 --- a/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount5.java +++ b/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount5.java @@ -5,7 +5,6 @@ import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.concurrent.ExecutionException; public class MultiThreadWordCount5 { // 使用threadNum个线程,并发统计文件中各单词的数量 From 0943fd92b43bd3d3e6242b78c0b9b4780782ee7a Mon Sep 17 00:00:00 2001 From: zaker-666 <1075151259china@gmail.com> Date: Sat, 11 Jan 2020 17:14:34 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E4=BA=86Pom=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 3 --- 1 file changed, 3 deletions(-) diff --git a/pom.xml b/pom.xml index 697b999..0610fd1 100644 --- a/pom.xml +++ b/pom.xml @@ -45,9 +45,6 @@ maven-surefire-plugin 2.22.1 - - -Dfile.encoding=UTF-8 - maven-checkstyle-plugin