在 10g 的 数字文件中 找出其中 最大的10位数字

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.stream.Stream;

//4050005120 1g
//1840653127 10g
public class Test {

    public static void main(String[] args) {
        HashMap> findIntegers = new HashMap<>();
        int threadAmount = 10;
        CountDownLatch countDownLatch = new CountDownLatch(threadAmount);
        try {
            String fileDire = "E:\\1g.txt";
//            String fileDire = "C:\\Users\\root\\Desktop\\aaa.txt";
            Stream lines = Files.lines(Paths.get(fileDire));
            long lineCount = lines.count();

            Test test = new Test();
            long subFilePartSize = test.getResultPartSize(lineCount, threadAmount);

//threadAmount + 1 多增加一个线程 计算剩余的 不足 subFilePartSize 的行
            if(lineCount > threadAmount* subFilePartSize){
                threadAmount++;
            }
            for (int i = 0; i < threadAmount; i++) {
                Stream finalLines = Files.lines(Paths.get(fileDire));
                int filePart = i;
                new Thread(() -> {
                    System.out.println(Thread.currentThread().getName() + " " + "start running");
                    Stream limit = finalLines.skip(subFilePartSize * filePart).limit(subFilePartSize);
                    List maxTopTenInteger = test.getMaxTopTenInteger(limit);

                    findIntegers.put(Thread.currentThread().getName(), maxTopTenInteger);
                    countDownLatch.countDown();

                    System.out.println(Thread.currentThread().getName()+ " " + "end running");
                }, "Thread " + i).start();

            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            countDownLatch.await();
            System.out.println(findIntegers);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }


    }

    private long getResultPartSize(long fileSize, int threadAmount) {
        return fileSize / threadAmount;
    }

    private List getMaxTopTenInteger(Stream limit) {
        List integers = new ArrayList<>();
        limit.forEach(s -> {
            Integer except = Integer.valueOf(s);
            if (integers.size() > 9) {
                compareAndSwap(integers, except);
            } else {
                integers.add(except);
            }
        });
        return integers;
    }

    private void compareAndSwap(List integers, Integer except) {
        for (int i = 0; i < integers.size(); i++) {
            Integer integer = integers.get(i);
            if (except > integer) {
                integers.set(i, except);
            }
        }
    }
}

 
  
    HashMap> findIntegers = new HashMap<>();

就是每个线程中取出的最大的前10 List
最后一步是 比较合并 findIntegers ,n 个list,merge成一个。 

你可能感兴趣的:(在 10g 的 数字文件中 找出其中 最大的10位数字)