From c02a4bf8c54379fc1384df2aa4f38d23e02a5f7d Mon Sep 17 00:00:00 2001 From: Yuyan <845058547@qq.com> Date: Thu, 12 Sep 2019 16:53:28 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E5=8D=95=E4=B8=AA=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E7=9A=84=E8=A0=A2=E5=8A=9E=E6=B3=95=E5=AE=9E=E7=8E=B0=E8=AF=A5?= =?UTF-8?q?=E5=8A=9F=E8=83=BD=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../github/hcsp/multithread/WordCount.java | 45 +++++++++++++++++-- 1 file changed, 42 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/github/hcsp/multithread/WordCount.java b/src/main/java/com/github/hcsp/multithread/WordCount.java index 51ab885..24e011b 100644 --- a/src/main/java/com/github/hcsp/multithread/WordCount.java +++ b/src/main/java/com/github/hcsp/multithread/WordCount.java @@ -1,13 +1,52 @@ package com.github.hcsp.multithread; import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.HashMap; +import java.util.List; import java.util.Map; +import java.util.StringTokenizer; public class WordCount { - public WordCount(int threadNum) {} + public WordCount(int threadNum) { + } // 统计文件中各单词的数量 - public Map count(File file) { - return null; + public Map count(File file) throws IOException { + Map numberOfWord = new HashMap<>(); + StringTokenizer stringTokenizer = dealWithStringFromFile(file); + while (stringTokenizer.hasMoreTokens()) { + String aWord = stringTokenizer.nextToken(); + if (numberOfWord.containsKey(aWord)) { + int count = numberOfWord.get(aWord); + numberOfWord.put(aWord, count + 1); + } else { + numberOfWord.put(aWord, 1); + } + } + + System.out.println(numberOfWord); + return numberOfWord; + } + + /** + * Processing the content of the file into a string + * + * @param file + * @return StringTokenizer + * @throws IOException + */ + + public static StringTokenizer dealWithStringFromFile(File file) throws IOException { + List stringFromFile = Files.readAllLines(file.toPath()); + StringTokenizer string; + string = new StringTokenizer(stringFromFile.toString() + .replace("[", " ") + .replace("]", " ") + .replace(",", " ")); + return string; + } } + From 4ad7518a61fe7f7397f9dd49011a793118a2de0d Mon Sep 17 00:00:00 2001 From: Yuyan <845058547@qq.com> Date: Thu, 12 Sep 2019 17:09:19 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E8=A7=A3=E5=86=B3=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E4=BA=9Bcheckstyle=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1.txt | 2 ++ src/main/java/com/github/hcsp/multithread/WordCount.java | 4 ++-- 2 files changed, 4 insertions(+), 2 deletions(-) create mode 100644 1.txt diff --git a/1.txt b/1.txt new file mode 100644 index 0000000..1a5d9f2 --- /dev/null +++ b/1.txt @@ -0,0 +1,2 @@ +i am a boy +i have a dog \ No newline at end of file diff --git a/src/main/java/com/github/hcsp/multithread/WordCount.java b/src/main/java/com/github/hcsp/multithread/WordCount.java index 24e011b..f24088c 100644 --- a/src/main/java/com/github/hcsp/multithread/WordCount.java +++ b/src/main/java/com/github/hcsp/multithread/WordCount.java @@ -33,9 +33,9 @@ public Map count(File file) throws IOException { /** * Processing the content of the file into a string * - * @param file + * @param file A file whose content is a character * @return StringTokenizer - * @throws IOException + * @throws IOException File is empty */ public static StringTokenizer dealWithStringFromFile(File file) throws IOException { From 40d159e6a9f17bada5bb88479efef9a1774c1fe5 Mon Sep 17 00:00:00 2001 From: Yuyan <845058547@qq.com> Date: Thu, 12 Sep 2019 17:14:54 +0800 Subject: [PATCH 3/5] =?UTF-8?q?.gitignore=E6=B2=A1=E8=B5=B7=E4=BD=9C?= =?UTF-8?q?=E7=94=A8=EF=BC=8C=E6=89=80=E4=BB=A5=E7=9B=B4=E6=8E=A5=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E4=BA=86=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 1.txt | 2 -- 1 file changed, 2 deletions(-) delete mode 100644 1.txt diff --git a/1.txt b/1.txt deleted file mode 100644 index 1a5d9f2..0000000 --- a/1.txt +++ /dev/null @@ -1,2 +0,0 @@ -i am a boy -i have a dog \ No newline at end of file From 0aeee896b30ce3ca5b7e769d52a08fe8176fecf1 Mon Sep 17 00:00:00 2001 From: Yuyan <845058547@qq.com> Date: Thu, 12 Sep 2019 19:35:18 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E4=BD=BF=E7=94=A8=E7=BA=BF=E7=A8=8B?= =?UTF-8?q?=E6=B1=A0=E5=AE=9E=E7=8E=B0=E8=AF=A5=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../github/hcsp/multithread/WordCount.java | 75 +++++++++++-------- 1 file changed, 42 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/github/hcsp/multithread/WordCount.java b/src/main/java/com/github/hcsp/multithread/WordCount.java index f24088c..8d72600 100644 --- a/src/main/java/com/github/hcsp/multithread/WordCount.java +++ b/src/main/java/com/github/hcsp/multithread/WordCount.java @@ -1,52 +1,61 @@ package com.github.hcsp.multithread; +import java.io.BufferedReader; import java.io.File; +import java.io.FileReader; import java.io.IOException; -import java.nio.file.Files; +import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; -import java.util.StringTokenizer; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; public class WordCount { + private final int threadNum; + private ExecutorService threadPool; + public WordCount(int threadNum) { + threadPool = Executors.newFixedThreadPool(threadNum); + this.threadNum = threadNum; } // 统计文件中各单词的数量 - public Map count(File file) throws IOException { - Map numberOfWord = new HashMap<>(); - StringTokenizer stringTokenizer = dealWithStringFromFile(file); - while (stringTokenizer.hasMoreTokens()) { - String aWord = stringTokenizer.nextToken(); - if (numberOfWord.containsKey(aWord)) { - int count = numberOfWord.get(aWord); - numberOfWord.put(aWord, count + 1); - } else { - numberOfWord.put(aWord, 1); - } + public Map count(File file) throws IOException, ExecutionException, InterruptedException { + BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); + List>> list = new ArrayList<>(); + Map finalResult = new HashMap<>(); + for (int i = 0; i < threadNum; i++) { + list.add(threadPool.submit(() -> { + Map result = new HashMap<>(); + while (!bufferedReader.readLine().isEmpty()) { + String line = bufferedReader.readLine(); + String[] oneThreadReadOneLine = line.split(""); + for (String element : oneThreadReadOneLine + ) { + result.put(element, result.getOrDefault(element, 1)); + } + } + return result; + })); } - - System.out.println(numberOfWord); - return numberOfWord; + for (Future> future : list + ) { + Map resultFromThread = future.get(); + mergeResultFromThread(resultFromThread, finalResult); + } + return finalResult; } - /** - * Processing the content of the file into a string - * - * @param file A file whose content is a character - * @return StringTokenizer - * @throws IOException File is empty - */ - - public static StringTokenizer dealWithStringFromFile(File file) throws IOException { - List stringFromFile = Files.readAllLines(file.toPath()); - StringTokenizer string; - string = new StringTokenizer(stringFromFile.toString() - .replace("[", " ") - .replace("]", " ") - .replace(",", " ")); - return string; - + private void mergeResultFromThread(Map resultFromThread, + Map finalResult) { + for (Map.Entry entry : resultFromThread.entrySet()) { + int resultNumber = finalResult.getOrDefault(entry.getKey(), 0) + entry.getValue(); + finalResult.put(entry.getKey(), resultNumber); + } } + } From 7842ff3861f48411876fff23d6e3e7ad16e30d15 Mon Sep 17 00:00:00 2001 From: Yuyan <845058547@qq.com> Date: Fri, 13 Sep 2019 09:25:11 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E6=94=B9=E6=AD=A3=E4=BA=86=E4=B8=80?= =?UTF-8?q?=E4=BA=9B=E9=94=99=E8=AF=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/github/hcsp/multithread/WordCount.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/java/com/github/hcsp/multithread/WordCount.java b/src/main/java/com/github/hcsp/multithread/WordCount.java index 8d72600..62e224d 100644 --- a/src/main/java/com/github/hcsp/multithread/WordCount.java +++ b/src/main/java/com/github/hcsp/multithread/WordCount.java @@ -27,20 +27,21 @@ public Map count(File file) throws IOException, ExecutionExcept BufferedReader bufferedReader = new BufferedReader(new FileReader(file)); List>> list = new ArrayList<>(); Map finalResult = new HashMap<>(); - for (int i = 0; i < threadNum; i++) { + for (int i = 0; i < threadNum; ++i) { list.add(threadPool.submit(() -> { Map result = new HashMap<>(); - while (!bufferedReader.readLine().isEmpty()) { - String line = bufferedReader.readLine(); - String[] oneThreadReadOneLine = line.split(""); + String line; + while ((line = bufferedReader.readLine())!=null) { + String[] oneThreadReadOneLine = line.split(" "); for (String element : oneThreadReadOneLine ) { - result.put(element, result.getOrDefault(element, 1)); + result.put(element, result.getOrDefault(element, 0)+1); } } return result; })); } + for (Future> future : list ) { Map resultFromThread = future.get(); @@ -56,6 +57,5 @@ private void mergeResultFromThread(Map resultFromThread, finalResult.put(entry.getKey(), resultNumber); } } - }