多线程读取文件,并统计文件中每种字母出现的频率

package com.tk.ConcurrentHashMap;

import lombok.extern.slf4j.Slf4j;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.function.BiConsumer;
import java.util.function.Supplier;
import java.util.stream.Stream;

/**
 * ConcurrentHashMap测试类
 * 多线程读取文件,并统计文件中每种字母出现的频率
 *
 * @author taoke
 * @date 2023/3/24
 */
@Slf4j

public class ConcurrentHashMapTest {
    /**
     * 测试
     *
     * @param args 参数
     */
    public static void main(String[] args) {
        handler(
                (Supplier>) ConcurrentHashMap::new,
                (map, words) -> {
                    for (String word : words) {
                        // 函数式编程,无需原子变量
                        map.merge(word, 1, Integer::sum);
                    }
                }
        );

    }

    /**
     * 统计文件中每个字母出现的频率
     *
     * @param supplier 提供者
     * @param consumer 消费者
     * @param       数据源
     */
    private static  void handler(Supplier> supplier,
                                    BiConsumer, List> consumer) {
        Map counterMap = supplier.get();
        List ts = new ArrayList<>();
        for (int i = 1; i <= 26; i++) {
            int j = i;
            Thread thread = new Thread(() -> {
                List words = readFromFile(j);
                consumer.accept(counterMap, words);
            });
            ts.add(thread);
        }
        ts.forEach(Thread::start);
        ts.forEach(t -> {
            try {
                t.join();
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
        System.out.println(counterMap);
    }

    /**
     * 将文件读取到list中
     *
     * @param index 文件序号
     * @return 数据源
     */
    public static List readFromFile(int index) {
        String path = "C:\\Users\\ketao\\Desktop\\study\\lock\\src\\main\\java\\com\\tk\\ConcurrentHashMap\\testData\\" + index + ".txt";
        List words = new ArrayList<>();
        try (Stream stream = Files.lines(Paths.get(path))) {
            stream.forEach(words::add);
            return words;
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}


你可能感兴趣的:(java)