-
Notifications
You must be signed in to change notification settings - Fork 82
提交了修改 #65
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
提交了修改 #65
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 { | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 名字取的不好,明明是读文件的,为啥叫 |
||
| List<Map<String, Integer>> result; | ||
| Object object; | ||
| File file; | ||
| CountDownLatch countDownLatch; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 这个CountDownLatch到处都没用到,那它为什么存在? |
||
|
|
||
| public FileDetector(List<Map<String, Integer>> countResults, Object object, File file) { | ||
| result = countResults; | ||
| this.object = object; | ||
| this.file = file; | ||
| } | ||
|
|
||
| public FileDetector(List<Map<String, Integer>> 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<String, Integer> 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(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -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<String, Integer> count(int threadNum, List<File> files) { | ||||||
| return null; | ||||||
| public static Map<String, Integer> count(int threadNum, List<File> files) throws ExecutionException, InterruptedException { | ||||||
| ExecutorService executorService = Executors.newFixedThreadPool(threadNum); | ||||||
| Map<String, Integer> countResult = new HashMap<>(); | ||||||
| List<Future<Map<String, Integer>>> futures = new ArrayList<>(); | ||||||
| for (File file : files) { | ||||||
| futures.add(executorService.submit(() -> { | ||||||
| Map<String, Integer> 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<String, Integer> waitforMerge; | ||||||
| for (Future<Map<String, Integer>> future : futures) { | ||||||
| waitforMerge = future.get(); | ||||||
| mergeResulttoFinal(waitforMerge, countResult); | ||||||
| } | ||||||
| return countResult; | ||||||
| } | ||||||
|
|
||||||
| private static void mergeResulttoFinal(Map<String, Integer> waitforMerge, Map<String, Integer> countResult) { | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| for (Map.Entry<String, Integer> entry : waitforMerge.entrySet()) { | ||||||
| String word = entry.getKey(); | ||||||
| int i = countResult.getOrDefault(word, 0) + entry.getValue(); | ||||||
| countResult.put(word, i); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<String, Integer> count(int threadNum, List<File> files) { | ||
| // return null; | ||
| // } | ||
| public static Map<String, Integer> count(int threadNum, List<File> files) throws InterruptedException { | ||
| List<Map<String, Integer>> results = new ArrayList<>(); | ||
| Map<String, Integer> countResult = new HashMap<>(); | ||
| buildDetector(threadNum, files, results); | ||
| Thread.sleep(100); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 你怎么知道sleep 100毫秒就足够其他线程完成工作? |
||
| for (Map<String, Integer> waitforMerge : results) { | ||
| mergeResulttoFinal(waitforMerge, countResult); | ||
| } | ||
| return countResult; | ||
| } | ||
|
|
||
| private static void buildDetector(int threadNum, List<File> files, List<Map<String, Integer>> results) { | ||
| for (int i = 0; i < threadNum; i++) { | ||
| new FileDetector(results, null, files.get(i)).start(); | ||
| } | ||
| } | ||
|
|
||
| private static void mergeResulttoFinal(Map<String, Integer> waitforMerge, Map<String, Integer> countResult) { | ||
| for (Map.Entry<String, Integer> entry : waitforMerge.entrySet()) { | ||
| String word = entry.getKey(); | ||
| int i = countResult.getOrDefault(word, 0) + entry.getValue(); | ||
| countResult.put(word, i); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<String, Integer> count(int threadNum, List<File> files) { | ||
| // return null; | ||
| // } | ||
| public static Map<String, Integer> count(int threadNum, List<File> files) throws InterruptedException { | ||
| final CountDownLatch countDownLatch = new CountDownLatch(threadNum); | ||
| List<Map<String, Integer>> results = new ArrayList<>(); | ||
| Map<String, Integer> countResult = new HashMap<>(); | ||
| buildDetector(threadNum, files, results, countDownLatch); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 我觉得你这里少了 |
||
| for (Map<String, Integer> waitforMerge : results) { | ||
| mergeResulttoFinal(waitforMerge, countResult); | ||
| } | ||
| return countResult; | ||
| } | ||
|
|
||
| private static void buildDetector(int threadNum, List<File> files, List<Map<String, Integer>> results, CountDownLatch countDownLatch) { | ||
| for (int i = 0; i < threadNum; i++) { | ||
| new FileDetector(results, null, files.get(i), countDownLatch).start(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. CountDownLatch在 |
||
| } | ||
| } | ||
|
|
||
| private static void mergeResulttoFinal(Map<String, Integer> waitforMerge, Map<String, Integer> countResult) { | ||
| for (Map.Entry<String, Integer> entry : waitforMerge.entrySet()) { | ||
| String word = entry.getKey(); | ||
| int i = countResult.getOrDefault(word, 0) + entry.getValue(); | ||
| countResult.put(word, i); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<String, Integer> count(int threadNum, List<File> files) { | ||
| // return null; | ||
| // } | ||
| public static Map<String, Integer> count(int threadNum, List<File> files) throws InterruptedException { | ||
| List<Map<String, Integer>> results = new ArrayList<>(); | ||
| Map<String, Integer> countResult = new HashMap<>(); | ||
| buildDetector(threadNum, files, results); | ||
| Thread.sleep(100); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 同上,为什么100毫秒就够了?假如正在处理的是很大的文件,100毫秒处理不完怎么办?这种拍脑袋的代码(“我猜100毫秒就够了”)请尽量少写。你需要的是正确的线程同步的方法,比如 |
||
| for (Map<String, Integer> waitforMerge : results) { | ||
| mergeResulttoFinal(waitforMerge, countResult); | ||
| } | ||
| return countResult; | ||
| } | ||
|
|
||
| private static void buildDetector(int threadNum, List<File> files, List<Map<String, Integer>> results) { | ||
| for (int i = 0; i < threadNum; i++) { | ||
| new FileDetector(results, null, files.get(i)).start(); | ||
| } | ||
| } | ||
|
|
||
| private static void mergeResulttoFinal(Map<String, Integer> waitforMerge, Map<String, Integer> countResult) { | ||
| for (Map.Entry<String, Integer> entry : waitforMerge.entrySet()) { | ||
| String word = entry.getKey(); | ||
| int i = countResult.getOrDefault(word, 0) + entry.getValue(); | ||
| countResult.put(word, i); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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<String, Integer> count(int threadNum, List<File> files) { | ||
| // return null; | ||
| // } | ||
| public static Map<String, Integer> count(int threadNum, List<File> files) throws InterruptedException { | ||
| List<Map<String, Integer>> results = new ArrayList<>(); | ||
| Map<String, Integer> countResult = new HashMap<>(); | ||
| buildDetector(threadNum, files, results); | ||
| Thread.sleep(100); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 同上。 |
||
| for (Map<String, Integer> waitforMerge : results) { | ||
| mergeResulttoFinal(waitforMerge, countResult); | ||
| } | ||
| return countResult; | ||
| } | ||
|
|
||
| private static void buildDetector(int threadNum, List<File> files, List<Map<String, Integer>> results) { | ||
| for (int i = 0; i < threadNum; i++) { | ||
| new FileDetector(results, null, files.get(i)).start(); | ||
| } | ||
| } | ||
|
|
||
| private static void mergeResulttoFinal(Map<String, Integer> waitforMerge, Map<String, Integer> countResult) { | ||
| for (Map.Entry<String, Integer> entry : waitforMerge.entrySet()) { | ||
| String word = entry.getKey(); | ||
| int i = countResult.getOrDefault(word, 0) + entry.getValue(); | ||
| countResult.put(word, i); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这里为什么要去掉?