Conversation
|
🎉 感谢提交Pull Request!请稍等片刻,我们已经将其提交到CI进行检查,一旦有结果会立即通知您! 我们不鼓励同时打开多个Pull Request,请集中精力于现在的这个Pull Request,谢谢!:pray: |
|
你的提交 ed04930 ,似乎失败了:Your tests failed on CircleCI 😅 请不要气馁,仔细分析原因,再接再厉! |
| @@ -1,14 +1,77 @@ | |||
| package com.github.hcsp.multithread; | |||
|
|
|||
| import com.sun.deploy.security.SelectableSecurityManager; | |||
| List<Future<Map<String, Integer>>> list = new ArrayList<>(); | ||
| Map<String, Integer> finalResult = new HashMap<>(); | ||
| for (int i = 0; i < threadNum; ++i) { | ||
| while (files.get(0).exists()) { |
There was a problem hiding this comment.
这里如果files为空呢?
| while (files.get(0).exists()) { | |
| while (!files.isEmpty()) { |
There was a problem hiding this comment.
我这样建议只是指出你的问题,并不代表这样改了你的程序就能正常工作了。你最好避免多个子线程同时操作List,原因是:
- 你的List没有同步。
- 你怎么保证List是可以修改的,就调用它的remove方法?
- 很容易出线程安全问题。
| } | ||
| } | ||
| files.remove(0); | ||
| threadPool.shutdown(); |
There was a problem hiding this comment.
每个子线程执行完就自己把自己的线程池关掉?这个操作很骚啊。
There was a problem hiding this comment.
虽然你从线程池中的线程里关闭肯定会遇到异常,但是我还是想问,线程1执行完了就把线程池整个关掉,那别的线程要是还在执行咋办?
|
好的,谢谢老师
…------------------ 原始邮件 ------------------
发件人: "Bo Zhang"<notifications@github.com>;
发送时间: 2019年9月14日(星期六) 下午2:30
收件人: "hcsp/multithread-word-count"<multithread-word-count@noreply.github.com>;
抄送: "MR.mister"<845058547@qq.com>; "Author"<author@noreply.github.com>;
主题: Re: [hcsp/multithread-word-count] 索引超限问题 (#9)
@blindpirate commented on this pull request.
最后的一个建议是,别写这么长的方法,因为很难理解,人类的思维可能跟不上。我的建议是,你声明一个这样的内部类:
static class Worker implements Callable<Map<String, Integer>> { List<File> 子线程分配到的一两个文件; }
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
|
应该放最外面去的
…------------------ 原始邮件 ------------------
发件人: "Bo Zhang"<notifications@github.com>;
发送时间: 2019年9月14日(星期六) 下午2:37
收件人: "hcsp/multithread-word-count"<multithread-word-count@noreply.github.com>;
抄送: "MR.mister"<845058547@qq.com>; "Author"<author@noreply.github.com>;
主题: Re: [hcsp/multithread-word-count] 索引超限问题 (#9)
@blindpirate commented on this pull request.
In src/main/java/com/github/hcsp/multithread/WordCount.java:
+ Map<String, Integer> finalResult = new HashMap<>(); + for (int i = 0; i < threadNum; ++i) { + while (files.get(0).exists()) { + File file = files.get(0); + list.add(threadPool.submit(() -> { + Map<String, Integer> result = new HashMap<>(); + List<String> oneFileToString = Files.readAllLines(file.toPath()); + for (String content : oneFileToString) { + String[] contentToArray = content.split(" "); + for (String element : contentToArray + ) { + result.put(element, result.getOrDefault(element, 0) + 1); + } + } + files.remove(0); + threadPool.shutdown();
虽然你从线程池中的线程里关闭肯定会遇到异常,但是我还是想问,线程1执行完了就把线程池整个关掉,那别的线程要是还在执行咋办?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
|
你的提交 ee933fd ,似乎失败了:Your tests failed on CircleCI 😅 请不要气馁,仔细分析原因,再接再厉! |
|
|
||
| for (Future<Map<String, Integer>> future : list | ||
| ) { | ||
| Map<String, Integer> resultFromThread = future.get(); |
There was a problem hiding this comment.
@blindpirate 老师,我调试到这里的时候,发现就只提示了一个 application is running了。想问下是为什么呢?
There was a problem hiding this comment.
你难道没注意到你的电脑CPU负载很高么?所有的线程都在跑死循环啊。。。
| } | ||
| } | ||
|
|
||
| private synchronized static List<List<File>> averageList(List<File> files, Integer number) { |
There was a problem hiding this comment.
我有没有跟你说过,不要自己造轮子?自己尝试搜索一下有没有类似方法?如果你需要的话,我可以允许你修改pom.xml.
| public Map<String, Integer> call() throws Exception { | ||
| Map<String, Integer> result = new ConcurrentHashMap<>(); | ||
| while (!files.isEmpty()) { | ||
| for (File file : files) { |
There was a problem hiding this comment.
这个方法的时间复杂度是太高了,最开始的时候我是用files.remove(index)读取list中的文件的,会报出ConcurrentModificationException,尝试了加锁,已经copyOnWriteArrayList几种方法都没效。
| @Override | ||
| public Map<String, Integer> call() throws Exception { | ||
| Map<String, Integer> result = new ConcurrentHashMap<>(); | ||
| while (!files.isEmpty()) { |
There was a problem hiding this comment.
把这个while去掉就好了。不明白为啥你要套两层循环:
whilefor (File file : files)
因为files永远不为空,所以这个循环永远不会结束。
|
gvava里面好像有一个,那我可以引用了
…------------------ 原始邮件 ------------------
发件人: "Bo Zhang"<notifications@github.com>;
发送时间: 2019年9月15日(星期天) 上午10:37
收件人: "hcsp/multithread-word-count"<multithread-word-count@noreply.github.com>;
抄送: "MR.mister"<845058547@qq.com>; "Author"<author@noreply.github.com>;
主题: Re: [hcsp/multithread-word-count] 索引超限问题 (#9)
@blindpirate commented on this pull request.
In src/main/java/com/github/hcsp/multithread/WordCount.java:
+ } + } + } + return result; + } + } + + private void mergeResultFromThread(Map<String, Integer> resultFromThread, + Map<String, Integer> finalResult) { + for (Map.Entry<String, Integer> entry : resultFromThread.entrySet()) { + int resultNumber = finalResult.getOrDefault(entry.getKey(), 0) + entry.getValue(); + finalResult.put(entry.getKey(), resultNumber); + } + } + + private synchronized static List<List<File>> averageList(List<File> files, Integer number) {
我有没有跟你说过,不要自己造轮子?自己尝试搜索一下有没有类似方法?如果你需要的话,我可以允许你修改pom.xml.
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
|
你的提交 a219aab ,似乎失败了:Your tests failed on CircleCI 😅 请不要气馁,仔细分析原因,再接再厉! |
|
你的提交 7dd9a6c ,似乎失败了:Your tests failed on CircleCI 😅 请不要气馁,仔细分析原因,再接再厉! |
|
你的提交 8500bed ,似乎失败了:Your tests failed on CircleCI 😅 请不要气馁,仔细分析原因,再接再厉! |
|
恭喜你,你的提交 9ebc882 已经通过我们的CI检查:Your tests passed on CircleCI! 👍 它会被自动merge后revert。请不要骄傲,继续挑战! |
| public class WordCount { | ||
| public WordCount(int threadNum) {} | ||
| private final int threadNum; | ||
| private ExecutorService threadPool; |
|
|
||
| @Override | ||
| public Map<String, Integer> call() throws Exception { | ||
| Map<String, Integer> result = new ConcurrentHashMap<>(); |
There was a problem hiding this comment.
其实这里就没必要用ConcurrentHashMap了,想一想为什么?
There was a problem hiding this comment.
因为这是一个局部变量啊,没有任何并发写操作发生啊。
|
因为已经加锁了吧
…---原始邮件---
发件人: "Bo Zhang"<notifications@github.com>
发送时间: 2019年9月15日(星期日) 下午3:34
收件人: "hcsp/multithread-word-count"<multithread-word-count@noreply.github.com>;
抄送: "JiangXi-YuYan"<845058547@qq.com>;"Author"<author@noreply.github.com>;
主题: Re: [hcsp/multithread-word-count] 索引超限问题 (#9)
@blindpirate commented on this pull request.
In src/main/java/com/github/hcsp/multithread/WordCount.java:
> + System.out.println(finalResult); + threadPool.shutdown(); + return finalResult; + } + } + + static class Worker implements Callable<Map<String, Integer>> { + List<File> files; + + Worker(List<File> files) { + this.files = files; + } + + @OverRide + public Map<String, Integer> call() throws Exception { + Map<String, Integer> result = new ConcurrentHashMap<>();
其实这里就没必要用ConcurrentHashMap了,想一想为什么?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub, or mute the thread.
|
有个索引超限问题不知道怎么解决