Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,56 @@
package com.github.hcsp.multithread;

import java.io.File;
import java.util.List;
import java.util.Map;
import java.io.*;
import java.util.*;
import java.util.concurrent.*;

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 IOException, ExecutionException, InterruptedException {
ExecutorService threadPool = Executors.newFixedThreadPool(threadNum);
HashMap<String, Integer> resultMap = new HashMap<>();

for (File file : files) {
HashMap<String, Integer> OneFileResultMap = new HashMap<>();
BufferedReader reader = new BufferedReader(new FileReader(file));
List<Future<Map<String, Integer>>> futureList = new ArrayList<>();

for (int i = 0; i < 0; i++) {
Future<Map<String, Integer>> future = threadPool.submit(new Callable<Map<String, Integer>>() {
@Override
public Map<String, Integer> call() throws Exception {
HashMap<String, Integer> oneLineMap = new HashMap<>();
String line = null;
while ((line = reader.readLine()) != null) {
String[] words = line.split(" ");
for (String word : words) {
oneLineMap.put(word, oneLineMap.getOrDefault(word, 0) + 1);
}
}
return oneLineMap;
}
});
futureList.add(future);
}
mergeOneLineCountToOneFileCount(OneFileResultMap, futureList);
mergeOneLineCountToOneFileCountByMap(resultMap, OneFileResultMap);
}
return resultMap;
}

private static void mergeOneLineCountToOneFileCountByMap(Map<String, Integer> resultMap,
Map<String, Integer> oneFileResultMap) {
Set<String> keys = oneFileResultMap.keySet();
for (String key : keys) {
resultMap.put(key, oneFileResultMap.getOrDefault(key, oneFileResultMap.get(key)));
}
}

private static void mergeOneLineCountToOneFileCount(HashMap<String, Integer> oneFileResultMap,
List<Future<Map<String, Integer>>> futureList) throws ExecutionException, InterruptedException {
for (Future<Map<String, Integer>> futures : futureList) {
Map<String, Integer> oneLineResult = futures.get();
mergeOneLineCountToOneFileCountByMap(oneFileResultMap, oneLineResult);
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,55 @@
package com.github.hcsp.multithread;

import java.io.*;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class MultiThreadWordCount2 {
// 使用threadNum个线程,并发统计文件中各单词的数量
// public static Map<String, Integer> count(int threadNum, List<File> files) {
// return null;
// }

//使用threadNum个线程,并发统计文件中各单词的数量
public static Map<String, Integer> count(int threadNum, List<File> files) throws IOException {
HashMap<String, Integer> resultMap = new HashMap<>();
for (File file : files) {
BufferedReader reader = new BufferedReader(new FileReader(file));
Object lock = new Object();
HashMap<String, Integer> oneFileResultMap = new HashMap<>();
for (int i = 0; i < 9; i++) {
new Thread(() -> {
synchronized (lock) {
String line = null;
while (true) {
try {
if (!((line = reader.readLine()) != null)) {
break;
}
} catch (IOException e) {
e.printStackTrace();
}
String[] words = line.split(" ");
for (String word : words) {
if (oneFileResultMap.containsKey(word)) {
oneFileResultMap.put(word, oneFileResultMap.get(word) + 1);
} else {
oneFileResultMap.put(word, 1);
}
}
}
}
});
}
mergeOneLineCountToOneFileCountByMap(resultMap, oneFileResultMap);
}
return resultMap;
}


private static void mergeOneLineCountToOneFileCountByMap(Map<String, Integer> resultMap,
Map<String, Integer> oneFileResultMap) {
Set<String> keys = oneFileResultMap.keySet();
for (String key : keys) {
resultMap.put(key, oneFileResultMap.getOrDefault(key, oneFileResultMap.get(key)));
}
}
}