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 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..8db27ad 100644 --- a/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount5.java +++ b/src/main/java/com/github/hcsp/multithread/MultiThreadWordCount5.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 MultiThreadWordCount5 { // 使用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); + } + } }